Mercurial > hg > octave-lyh
annotate scripts/general/bitcmp.m @ 10635:d1978e7364ad
Print name of function in error() string messages.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sun, 16 May 2010 22:26:54 -0700 |
parents | 1bf0ce0930be |
children | c776f063fefe |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2004, 2005, 2006, 2007, 2009 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 | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
25 ## @group |
4916 | 26 ## bitcmp(7,4) |
27 ## @result{} 8 | |
28 ## dec2bin(11) | |
29 ## @result{} 1011 | |
6515 | 30 ## dec2bin(bitcmp(11, 6)) |
31 ## @result{} 110100 | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
32 ## @end group |
4916 | 33 ## @end example |
5642 | 34 ## @seealso{bitand, bitor, bitxor, bitset, bitget, bitcmp, bitshift, bitmax} |
5053 | 35 ## @end deftypefn |
4916 | 36 |
37 ## Liberally based of the version by Kai Habel from octave-forge | |
38 | |
6515 | 39 function x = bitcmp (a, n) |
4916 | 40 |
41 if (nargin < 1 || nargin > 2) | |
6046 | 42 print_usage (); |
4916 | 43 endif |
44 | |
6515 | 45 if (nargin == 2 && (! isscalar (n) || (floor (n) != n))) |
10635
d1978e7364ad
Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
46 error ("bitcmp: k must be a scalar integer"); |
6515 | 47 endif |
48 | |
49 if (isa (a, "double")) | |
50 bmax = bitmax; | |
7333 | 51 amax = ceil (log2 (bmax)); |
4916 | 52 else |
6515 | 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; | |
4950 | 69 else |
10635
d1978e7364ad
Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
70 error ("bitcmp: invalid class %s", class (a)); |
4950 | 71 endif |
6515 | 72 bmax = intmax (class (a)); |
4916 | 73 endif |
74 | |
6515 | 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)) | |
10635
d1978e7364ad
Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents:
9051
diff
changeset
|
80 error ("bitcmp: n must be in the range [1,%d]", amax); |
4916 | 81 endif |
6515 | 82 mask = bitshift (bmax, n - amax); |
83 x = bitxor (bitand (a, mask), mask); | |
4916 | 84 endif |
6515 | 85 endfunction |
4916 | 86 |
6515 | 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)); |