7017
|
1 ## Copyright (C) 2004, 2005, 2006, 2007 David Bateman |
4916
|
2 ## |
7016
|
3 ## This file is part of Octave. |
4916
|
4 ## |
7016
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
4916
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
4916
|
18 |
|
19 ## -*- texinfo -*- |
6515
|
20 ## @deftypefn {Function File} {} bitcmp (@var{a}, @var{k}) |
4920
|
21 ## Return the @var{k}-bit complement of integers in @var{a}. If |
6515
|
22 ## @var{k} is omitted @code{k = log2 (bitmax) + 1} is assumed. |
4916
|
23 ## |
|
24 ## @example |
|
25 ## bitcmp(7,4) |
|
26 ## @result{} 8 |
|
27 ## dec2bin(11) |
|
28 ## @result{} 1011 |
6515
|
29 ## dec2bin(bitcmp(11, 6)) |
|
30 ## @result{} 110100 |
4916
|
31 ## @end example |
5642
|
32 ## @seealso{bitand, bitor, bitxor, bitset, bitget, bitcmp, bitshift, bitmax} |
5053
|
33 ## @end deftypefn |
4916
|
34 |
|
35 ## Liberally based of the version by Kai Habel from octave-forge |
|
36 |
6515
|
37 function x = bitcmp (a, n) |
4916
|
38 |
|
39 if (nargin < 1 || nargin > 2) |
6046
|
40 print_usage (); |
4916
|
41 endif |
|
42 |
6515
|
43 if (nargin == 2 && (! isscalar (n) || (floor (n) != n))) |
|
44 error("k must be a scalar integer") |
|
45 endif |
|
46 |
|
47 if (isa (a, "double")) |
|
48 bmax = bitmax; |
7332
|
49 amax = round (log2 (bmax)); |
4916
|
50 else |
6515
|
51 if (isa (a, "uint8")) |
|
52 amax = 8; |
|
53 elseif (isa (a, "uint16")) |
|
54 amax = 16; |
|
55 elseif (isa (a, "uint32")) |
|
56 amax = 32; |
|
57 elseif (isa (a, "uint64")) |
|
58 amax = 64; |
|
59 elseif (isa (a, "int8")) |
|
60 amax = 8; |
|
61 elseif (isa (a, "int16")) |
|
62 amax = 16; |
|
63 elseif (isa (a, "int32")) |
|
64 amax = 32; |
|
65 elseif (isa (a, "int64")) |
|
66 amax = 64; |
4950
|
67 else |
6515
|
68 error ("invalid class %s", class (a)); |
4950
|
69 endif |
6515
|
70 bmax = intmax (class (a)); |
4916
|
71 endif |
|
72 |
6515
|
73 if (nargin == 1 || n == amax) |
|
74 x = bitxor (a, bmax); |
|
75 else |
|
76 m = double (n); |
|
77 if (any (m < 1) || any (m > amax)) |
|
78 error ("n must be in the range [1,%d]", amax); |
4916
|
79 endif |
6515
|
80 mask = bitshift (bmax, n - amax); |
|
81 x = bitxor (bitand (a, mask), mask); |
4916
|
82 endif |
6515
|
83 endfunction |
4916
|
84 |
6515
|
85 %!shared Amax,Bmax,A |
|
86 %! Amax=53; |
|
87 %! Bmax = bitmax; |
|
88 %! A = bitshift(Bmax,-2); |
|
89 %!assert(bitcmp(A,Amax),bitor(bitshift(1,Amax-1),bitshift(1,Amax-2))); |
|
90 %!assert(bitcmp(A,Amax-1),bitshift(1,Amax-2)); |
|
91 %!assert(bitcmp(A,Amax-2),0); |
|
92 %!shared Amax,Bmax,A |
|
93 %! Amax=8; |
|
94 %! Bmax = intmax('uint8'); |
|
95 %! A = bitshift(Bmax,-2); |
|
96 %!assert(bitcmp(A,Amax),bitor(bitshift(uint8(1),Amax-1),bitshift(uint8(1),Amax-2))); |
|
97 %!assert(bitcmp(A,Amax-1),bitshift(uint8(1),Amax-2)); |
|
98 %!assert(bitcmp(A,Amax-2),uint8(0)); |
|
99 %!shared Amax,Bmax,A |
|
100 %! Amax=16; |
|
101 %! Bmax = intmax('uint16'); |
|
102 %! A = bitshift(Bmax,-2); |
|
103 %!assert(bitcmp(A,Amax),bitor(bitshift(uint16(1),Amax-1),bitshift(uint16(1),Amax-2))); |
|
104 %!assert(bitcmp(A,Amax-1),bitshift(uint16(1),Amax-2)); |
|
105 %!assert(bitcmp(A,Amax-2),uint16(0)); |
|
106 %!shared Amax,Bmax,A |
|
107 %! Amax=32; |
|
108 %! Bmax = intmax('uint32'); |
|
109 %! A = bitshift(Bmax,-2); |
|
110 %!assert(bitcmp(A,Amax),bitor(bitshift(uint32(1),Amax-1),bitshift(uint32(1),Amax-2))); |
|
111 %!assert(bitcmp(A,Amax-1),bitshift(uint32(1),Amax-2)); |
|
112 %!assert(bitcmp(A,Amax-2),uint32(0)); |
|
113 %!shared Amax,Bmax,A |
|
114 %! Amax=64; |
|
115 %! Bmax = intmax('uint64'); |
|
116 %! A = bitshift(Bmax,-2); |
|
117 %!assert(bitcmp(A,Amax),bitor(bitshift(uint64(1),Amax-1),bitshift(uint64(1),Amax-2))); |
|
118 %!assert(bitcmp(A,Amax-1),bitshift(uint64(1),Amax-2)); |
|
119 %!assert(bitcmp(A,Amax-2),uint64(0)); |