annotate scripts/general/bitcmp.m @ 12604:132c89bb44e3

maint: Refactor general/isXXX.m scripts to put input validation first. * general/isdir.m, general/isequal.m, general/isequalwithequalnans.m, general/isscalar.m, general/issquare.m, general/isvector.m: Refactor code to put input validation first. * general/iscolumn.m, general/isrow.m : Remove useless initialization of output variable. * general/isa.m: Add additional tests for various classes.
author Rik <octave@nomad.inbox5.com>
date Wed, 13 Apr 2011 09:32:10 -0700
parents 139f993936af
children 72c96de7a403
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11523
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 11472
diff changeset
1 ## Copyright (C) 2004-2011 David Bateman
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
2 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6515
diff changeset
3 ## This file is part of Octave.
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
4 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6515
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6515
diff changeset
6 ## under the terms of the GNU General Public License as published by
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6515
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6515
diff changeset
8 ## your option) any later version.
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6515
diff changeset
9 ##
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6515
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6515
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6515
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6515
diff changeset
13 ## General Public License for more details.
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
14 ##
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6515
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6515
diff changeset
17 ## <http://www.gnu.org/licenses/>.
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
18
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
20 ## @deftypefn {Function File} {} bitcmp (@var{A}, @var{k})
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
21 ## Return the @var{k}-bit complement of integers in @var{A}. If
6515
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
22 ## @var{k} is omitted @code{k = log2 (bitmax) + 1} is assumed.
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
23 ##
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
24 ## @example
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
25 ## @group
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
26 ## bitcmp(7,4)
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
27 ## @result{} 8
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
28 ## dec2bin(11)
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
29 ## @result{} 1011
6515
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
30 ## dec2bin(bitcmp(11, 6))
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
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
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
33 ## @end example
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5307
diff changeset
34 ## @seealso{bitand, bitor, bitxor, bitset, bitget, bitcmp, bitshift, bitmax}
5053
c08cb1098afc [project @ 2004-10-19 23:10:54 by jwe]
jwe
parents: 4950
diff changeset
35 ## @end deftypefn
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
36
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
37 ## Liberally based on the version by Kai Habel from octave-forge
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
38
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
39 function C = bitcmp (A, k)
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
40
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
41 if (nargin < 1 || nargin > 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
42 print_usage ();
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
43 endif
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
44
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
45 if (nargin == 2 && (! isscalar (k) || (floor (k) != k)))
11472
1740012184f9 Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents: 11469
diff changeset
46 error ("bitcmp: K must be a scalar integer");
6515
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
47 endif
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
48
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
49 if (isa (A, "double"))
6515
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
50 bmax = bitmax;
7333
66dfb0abd3d3 [project @ 2007-12-21 21:22:43 by jwe]
jwe
parents: 7332
diff changeset
51 amax = ceil (log2 (bmax));
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
52 else
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
53 if (isa (A, "uint8"))
6515
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
54 amax = 8;
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
55 elseif (isa (A, "uint16"))
6515
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
56 amax = 16;
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
57 elseif (isa (A, "uint32"))
6515
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
58 amax = 32;
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
59 elseif (isa (A, "uint64"))
6515
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
60 amax = 64;
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
61 elseif (isa (A, "int8"))
6515
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
62 amax = 8;
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
63 elseif (isa (A, "int16"))
6515
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
64 amax = 16;
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
65 elseif (isa (A, "int32"))
6515
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
66 amax = 32;
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
67 elseif (isa (A, "int64"))
6515
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
68 amax = 64;
4950
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
69 else
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
70 error ("bitcmp: invalid class %s", class (A));
4950
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
71 endif
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
72 bmax = intmax (class (A));
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
73 endif
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
74
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
75 if (nargin == 1 || k == amax)
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
76 C = bitxor (A, bmax);
6515
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
77 else
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
78 m = double (k);
6515
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
79 if (any (m < 1) || any (m > amax))
12480
139f993936af Uppercase variables in script error strings.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
80 error ("bitcmp: K must be in the range [1,%d]", amax);
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
81 endif
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
82 mask = bitshift (bmax, k - amax);
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
83 C = bitxor (bitand (A, mask), mask);
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
84 endif
6515
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
85 endfunction
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
86
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
87
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
88 %!test
6515
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
89 %! Amax=53;
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
90 %! Bmax = bitmax;
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
91 %! A = bitshift(Bmax,-2);
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
92 %! assert(bitcmp(A,Amax),bitor(bitshift(1,Amax-1),bitshift(1,Amax-2)));
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
93 %! assert(bitcmp(A,Amax-1),bitshift(1,Amax-2));
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
94 %! assert(bitcmp(A,Amax-2),0);
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
95 %!test
6515
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
96 %! Amax=8;
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
97 %! Bmax = intmax('uint8');
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
98 %! A = bitshift(Bmax,-2);
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
99 %! assert(bitcmp(A,Amax),bitor(bitshift(uint8(1),Amax-1),bitshift(uint8(1),Amax-2)));
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
100 %! assert(bitcmp(A,Amax-1),bitshift(uint8(1),Amax-2));
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
101 %! assert(bitcmp(A,Amax-2),uint8(0));
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
102 %!test
6515
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
103 %! Amax=16;
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
104 %! Bmax = intmax('uint16');
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
105 %! A = bitshift(Bmax,-2);
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
106 %! assert(bitcmp(A,Amax),bitor(bitshift(uint16(1),Amax-1),bitshift(uint16(1),Amax-2)));
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
107 %! assert(bitcmp(A,Amax-1),bitshift(uint16(1),Amax-2));
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
108 %! assert(bitcmp(A,Amax-2),uint16(0));
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
109 %!test
6515
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
110 %! Amax=32;
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
111 %! Bmax = intmax('uint32');
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
112 %! A = bitshift(Bmax,-2);
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
113 %! assert(bitcmp(A,Amax),bitor(bitshift(uint32(1),Amax-1),bitshift(uint32(1),Amax-2)));
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
114 %! assert(bitcmp(A,Amax-1),bitshift(uint32(1),Amax-2));
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
115 %! assert(bitcmp(A,Amax-2),uint32(0));
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
116 %!test
6515
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
117 %! Amax=64;
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
118 %! Bmax = intmax('uint64');
fc527bc3ece1 [project @ 2007-04-11 14:20:39 by jwe]
jwe
parents: 6046
diff changeset
119 %! A = bitshift(Bmax,-2);
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
120 %! assert(bitcmp(A,Amax),bitor(bitshift(uint64(1),Amax-1),bitshift(uint64(1),Amax-2)));
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
121 %! assert(bitcmp(A,Amax-1),bitshift(uint64(1),Amax-2));
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
122 %! assert(bitcmp(A,Amax-2),uint64(0));
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10635
diff changeset
123