1 ## Copyright (C) 2004-2012 David Bateman
3 ## This file is part of Octave.
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.
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.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} bitcmp (@var{A}, @var{k})
21 ## Return the @var{k}-bit complement of integers in @var{A}. If
22 ## @var{k} is omitted @code{k = log2 (bitmax) + 1} is assumed.
30 ## dec2bin (bitcmp (11, 6))
34 ## @seealso{bitand, bitor, bitxor, bitset, bitget, bitcmp, bitshift, bitmax}
37 ## Liberally based on the version by Kai Habel from octave-forge
39 function C = bitcmp (A, k)
41 if (nargin < 1 || nargin > 2)
45 if (nargin == 2 && (! isscalar (k) || (floor (k) != k)))
46 error ("bitcmp: K must be a scalar integer");
49 if (isa (A, "double"))
51 amax = ceil (log2 (bmax));
55 elseif (isa (A, "uint16"))
57 elseif (isa (A, "uint32"))
59 elseif (isa (A, "uint64"))
61 elseif (isa (A, "int8"))
63 elseif (isa (A, "int16"))
65 elseif (isa (A, "int32"))
67 elseif (isa (A, "int64"))
70 error ("bitcmp: invalid class %s", class (A));
72 bmax = intmax (class (A));
75 if (nargin == 1 || k == amax)
79 if (any (m < 1) || any (m > amax))
80 error ("bitcmp: K must be in the range [1,%d]", amax);
82 mask = bitshift (bmax, k - amax);
83 C = bitxor (bitand (A, mask), mask);
91 %! A = bitshift(Bmax,-2);
92 %! assert(bitcmp(A,Amax),bitor(bitshift(1,Amax-1),bitshift(1,Amax-2)));
93 %! assert(bitcmp(A,Amax-1),bitshift(1,Amax-2));
94 %! assert(bitcmp(A,Amax-2),0);
97 %! Bmax = intmax('uint8');
98 %! A = bitshift(Bmax,-2);
99 %! assert(bitcmp(A,Amax),bitor(bitshift(uint8(1),Amax-1),bitshift(uint8(1),Amax-2)));
100 %! assert(bitcmp(A,Amax-1),bitshift(uint8(1),Amax-2));
101 %! assert(bitcmp(A,Amax-2),uint8(0));
104 %! Bmax = intmax('uint16');
105 %! A = bitshift(Bmax,-2);
106 %! assert(bitcmp(A,Amax),bitor(bitshift(uint16(1),Amax-1),bitshift(uint16(1),Amax-2)));
107 %! assert(bitcmp(A,Amax-1),bitshift(uint16(1),Amax-2));
108 %! assert(bitcmp(A,Amax-2),uint16(0));
111 %! Bmax = intmax('uint32');
112 %! A = bitshift(Bmax,-2);
113 %! assert(bitcmp(A,Amax),bitor(bitshift(uint32(1),Amax-1),bitshift(uint32(1),Amax-2)));
114 %! assert(bitcmp(A,Amax-1),bitshift(uint32(1),Amax-2));
115 %! assert(bitcmp(A,Amax-2),uint32(0));
118 %! Bmax = intmax('uint64');
119 %! A = bitshift(Bmax,-2);
120 %! assert(bitcmp(A,Amax),bitor(bitshift(uint64(1),Amax-1),bitshift(uint64(1),Amax-2)));
121 %! assert(bitcmp(A,Amax-1),bitshift(uint64(1),Amax-2));
122 %! assert(bitcmp(A,Amax-2),uint64(0));