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 |
|
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
16 |
|
17 ## -*- texinfo -*- |
|
18 ## @deftypefn {Function File} {@var{X} =} bitcmp (@var{a},@var{k}) |
4920
|
19 ## Return the @var{k}-bit complement of integers in @var{a}. If |
4916
|
20 ## @var{k} is omitted @code{k = log2(bitmax) + 1} is assumed. |
|
21 ## |
|
22 ## @example |
|
23 ## bitcmp(7,4) |
|
24 ## @result{} 8 |
|
25 ## dec2bin(11) |
|
26 ## @result{} 1011 |
|
27 ## dec2bin(bitcmp(11)) |
|
28 ## @result{} 11111111111111111111111111110100 |
|
29 ## @end example |
|
30 ## |
|
31 ## @seealso{bitand,bitor,bitxor,bitset,bitget,bitcmp,bitshift,bitmax} |
|
32 ## @end deftypefn |
|
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 |
4920
|
42 cname = class (A); |
4916
|
43 if (strcmp (cname, "double")) |
|
44 Bmax = bitmax; |
|
45 Amax = log2 (Bmax) + 1; |
4920
|
46 elseif strcmp ("uint", substr (cname, 1, 4)) |
|
47 Bmax = intmax (cname); |
|
48 Amax = eval ([cname, " (log2 (double (intmax (cname))) + 1);"]); |
4916
|
49 else |
4920
|
50 Bmax = eval ([cname, " (-1);"]); |
|
51 Amax = eval ([cname, " (log2 (double (intmax (cname))) + 2);"]); |
4916
|
52 endif |
|
53 |
4920
|
54 Aone = eval ([ cname, "(1);"]); |
4916
|
55 if (nargin == 2) |
4920
|
56 m = eval ([cname, " (n(:));"]); |
|
57 if (any (m < Aone) || any (m > Amax)) |
4916
|
58 error ("n must be in the range [1,%d]", Amax); |
|
59 endif |
4920
|
60 X = bitxor (A, bitshift (Bmax, -n)); |
4916
|
61 else |
4920
|
62 X = bitxor (A, Bmax); |
4916
|
63 endif |
|
64 |
|
65 endfunction |