changelog shortlog tags changeset files revisions annotate raw

scripts/general/bitcmp.m

changeset 10289: 4b124317dc38
parent:eb63fbe60fab
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (37 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 2004, 2005, 2006, 2007, 2009 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 of the version by Kai Habel from octave-forge
38
39function x = bitcmp (a, n)
40
41 if (nargin < 1 || nargin > 2)
42 print_usage ();
43 endif
44
45 if (nargin == 2 && (! isscalar (n) || (floor (n) != n)))
46 error ("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 ("invalid class %s", class (a));
71 endif
72 bmax = intmax (class (a));
73 endif
74
75 if (nargin == 1 || n == amax)
76 x = bitxor (a, bmax);
77 else
78 m = double (n);
79 if (any (m < 1) || any (m > amax))
80 error ("n must be in the range [1,%d]", amax);
81 endif
82 mask = bitshift (bmax, n - amax);
83 x = bitxor (bitand (a, mask), mask);
84 endif
85endfunction
86
87%!shared Amax,Bmax,A
88%! Amax=53;
89%! Bmax = bitmax;
90%! A = bitshift(Bmax,-2);
91%!assert(bitcmp(A,Amax),bitor(bitshift(1,Amax-1),bitshift(1,Amax-2)));
92%!assert(bitcmp(A,Amax-1),bitshift(1,Amax-2));
93%!assert(bitcmp(A,Amax-2),0);
94%!shared Amax,Bmax,A
95%! Amax=8;
96%! Bmax = intmax('uint8');
97%! A = bitshift(Bmax,-2);
98%!assert(bitcmp(A,Amax),bitor(bitshift(uint8(1),Amax-1),bitshift(uint8(1),Amax-2)));
99%!assert(bitcmp(A,Amax-1),bitshift(uint8(1),Amax-2));
100%!assert(bitcmp(A,Amax-2),uint8(0));
101%!shared Amax,Bmax,A
102%! Amax=16;
103%! Bmax = intmax('uint16');
104%! A = bitshift(Bmax,-2);
105%!assert(bitcmp(A,Amax),bitor(bitshift(uint16(1),Amax-1),bitshift(uint16(1),Amax-2)));
106%!assert(bitcmp(A,Amax-1),bitshift(uint16(1),Amax-2));
107%!assert(bitcmp(A,Amax-2),uint16(0));
108%!shared Amax,Bmax,A
109%! Amax=32;
110%! Bmax = intmax('uint32');
111%! A = bitshift(Bmax,-2);
112%!assert(bitcmp(A,Amax),bitor(bitshift(uint32(1),Amax-1),bitshift(uint32(1),Amax-2)));
113%!assert(bitcmp(A,Amax-1),bitshift(uint32(1),Amax-2));
114%!assert(bitcmp(A,Amax-2),uint32(0));
115%!shared Amax,Bmax,A
116%! Amax=64;
117%! Bmax = intmax('uint64');
118%! A = bitshift(Bmax,-2);
119%!assert(bitcmp(A,Amax),bitor(bitshift(uint64(1),Amax-1),bitshift(uint64(1),Amax-2)));
120%!assert(bitcmp(A,Amax-1),bitshift(uint64(1),Amax-2));
121%!assert(bitcmp(A,Amax-2),uint64(0));