4916
|
1 ## Copyright (C) 2004 David Bateman |
|
2 ## |
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2 of the License, or |
|
6 ## (at your option) any later version. |
|
7 ## |
|
8 ## This program is distributed in the hope that it will be useful, |
|
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 ## GNU General Public License for more details. |
|
12 ## |
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this program; if not, write to the Free Software |
5307
|
15 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
16 ## 02110-1301 USA |
4916
|
17 |
|
18 ## -*- texinfo -*- |
|
19 ## @deftypefn {Function File} {@var{X} =} bitcmp (@var{a},@var{k}) |
4920
|
20 ## Return the @var{k}-bit complement of integers in @var{a}. If |
4916
|
21 ## @var{k} is omitted @code{k = log2(bitmax) + 1} is assumed. |
|
22 ## |
|
23 ## @example |
|
24 ## bitcmp(7,4) |
|
25 ## @result{} 8 |
|
26 ## dec2bin(11) |
|
27 ## @result{} 1011 |
|
28 ## dec2bin(bitcmp(11)) |
|
29 ## @result{} 11111111111111111111111111110100 |
|
30 ## @end example |
5642
|
31 ## @seealso{bitand, bitor, bitxor, bitset, bitget, bitcmp, bitshift, bitmax} |
5053
|
32 ## @end deftypefn |
4916
|
33 |
|
34 ## Liberally based of the version by Kai Habel from octave-forge |
|
35 |
|
36 function X = bitcmp (A, n) |
|
37 |
|
38 if (nargin < 1 || nargin > 2) |
4920
|
39 usage ("bitcmp (A, n)"); |
4916
|
40 endif |
|
41 |
4950
|
42 if (isa (A, "double")) |
4916
|
43 Bmax = bitmax; |
|
44 Amax = log2 (Bmax) + 1; |
4950
|
45 _conv = @double; |
4916
|
46 else |
4950
|
47 if (isa (A, "uint8")) |
|
48 Amax = 8; |
|
49 _conv = @uint8; |
|
50 elseif (isa (A, "uint16")) |
|
51 Amax = 16; |
|
52 _conv = @uint16; |
|
53 elseif (isa (A, "uint32")) |
|
54 Amax = 32; |
|
55 _conv = @uint32; |
|
56 elseif (isa (A, "uint64")) |
|
57 Amax = 64; |
|
58 _conv = @uint64; |
|
59 elseif (isa (A, "int8")) |
|
60 Amax = 8; |
|
61 _conv = @int8; |
|
62 elseif (isa (A, "int16")) |
|
63 Amax = 16; |
|
64 _conv = @int16; |
|
65 elseif (isa (A, "int32")) |
|
66 Amax = 32; |
|
67 _conv = @int32; |
|
68 elseif (isa (A, "int64")) |
|
69 Amax = 64; |
|
70 _conv = @int64; |
|
71 else |
|
72 error ("invalid class %s", class (A)); |
|
73 endif |
|
74 Bmax = intmax (class (A)); |
4916
|
75 endif |
|
76 |
|
77 if (nargin == 2) |
4950
|
78 m = double (n(:)); |
|
79 if (any (m < 1) || any (m > Amax)) |
4916
|
80 error ("n must be in the range [1,%d]", Amax); |
|
81 endif |
4950
|
82 X = bitxor (A, bitshift (Bmax, -int8(n))); |
4916
|
83 else |
4920
|
84 X = bitxor (A, Bmax); |
4916
|
85 endif |
|
86 |
|
87 endfunction |