]> Creatis software - CreaPhase.git/blob - octave_packages/m/general/bitcmp.m
update packages
[CreaPhase.git] / octave_packages / m / general / bitcmp.m
1 ## Copyright (C) 2004-2012 David Bateman
2 ##
3 ## This file is part of Octave.
4 ##
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.
14 ##
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/>.
18
19 ## -*- texinfo -*-
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.
23 ##
24 ## @example
25 ## @group
26 ## bitcmp (7,4)
27 ##   @result{} 8
28 ## dec2bin (11)
29 ##   @result{} 1011
30 ## dec2bin (bitcmp (11, 6))
31 ##   @result{} 110100
32 ## @end group
33 ## @end example
34 ## @seealso{bitand, bitor, bitxor, bitset, bitget, bitcmp, bitshift, bitmax}
35 ## @end deftypefn
36
37 ## Liberally based on the version by Kai Habel from octave-forge
38
39 function C = bitcmp (A, k)
40
41   if (nargin < 1 || nargin > 2)
42     print_usage ();
43   endif
44
45   if (nargin == 2 && (! isscalar (k) || (floor (k) != k)))
46     error ("bitcmp: K must be a scalar integer");
47   endif
48
49   if (isa (A, "double"))
50     bmax = bitmax;
51     amax = ceil (log2 (bmax));
52   else
53     if (isa (A, "uint8"))
54       amax = 8;
55     elseif (isa (A, "uint16"))
56       amax = 16;
57     elseif (isa (A, "uint32"))
58       amax = 32;
59     elseif (isa (A, "uint64"))
60       amax = 64;
61     elseif (isa (A, "int8"))
62       amax = 8;
63     elseif (isa (A, "int16"))
64       amax = 16;
65     elseif (isa (A, "int32"))
66       amax = 32;
67     elseif (isa (A, "int64"))
68       amax = 64;
69     else
70       error ("bitcmp: invalid class %s", class (A));
71     endif
72     bmax = intmax (class (A));
73   endif
74
75   if (nargin == 1 || k == amax)
76     C = bitxor (A, bmax);
77   else
78     m = double (k);
79     if (any (m < 1) || any (m > amax))
80       error ("bitcmp: K must be in the range [1,%d]", amax);
81     endif
82     mask = bitshift (bmax, k - amax);
83     C = bitxor (bitand (A, mask), mask);
84   endif
85 endfunction
86
87
88 %!test
89 %! Amax=53;
90 %! Bmax = bitmax;
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);
95 %!test
96 %! Amax=8;
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));
102 %!test
103 %! Amax=16;
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));
109 %!test
110 %! Amax=32;
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));
116 %!test
117 %! Amax=64;
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));
123