Mercurial > hg > octave-nkf
annotate scripts/general/bitcmp.m @ 17987:edaa9ed49a1f
pt_BR.ts: Update Octave Portugese/Brazil translation.
* pt_BR.ts: Update Octave Portugese/Brazil translation.
author | Júlio Hoffimann Mendes <julio.hoffimann@gmail.com> |
---|---|
date | Sat, 23 Nov 2013 08:09:22 -0800 |
parents | d63878346099 |
children | 79d4783a9978 |
rev | line source |
---|---|
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
17392
diff
changeset
|
1 ## Copyright (C) 2004-2013 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 -*- | |
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 | 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 |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
26 ## bitcmp (7,4) |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
27 ## @result{} 8 |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
28 ## dec2bin (11) |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
29 ## @result{} 1011 |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
30 ## dec2bin (bitcmp (11, 6)) |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
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 | 33 ## @end example |
5642 | 34 ## @seealso{bitand, bitor, bitxor, bitset, bitget, bitcmp, bitshift, bitmax} |
5053 | 35 ## @end deftypefn |
4916 | 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 | 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 | 41 if (nargin < 1 || nargin > 2) |
6046 | 42 print_usage (); |
4916 | 43 endif |
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 | 47 endif |
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 | 50 bmax = bitmax; |
7333 | 51 amax = ceil (log2 (bmax)); |
17392
e09cd91168d1
Support arguments of class single in bit manipulation functions (bug #34502)
Mike Miller <mtmiller@ieee.org>
parents:
14363
diff
changeset
|
52 elseif (isa (A, "single")) |
e09cd91168d1
Support arguments of class single in bit manipulation functions (bug #34502)
Mike Miller <mtmiller@ieee.org>
parents:
14363
diff
changeset
|
53 bmax = bitmax ("single"); |
e09cd91168d1
Support arguments of class single in bit manipulation functions (bug #34502)
Mike Miller <mtmiller@ieee.org>
parents:
14363
diff
changeset
|
54 amax = ceil (log2 (bmax)); |
4916 | 55 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
|
56 if (isa (A, "uint8")) |
6515 | 57 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
|
58 elseif (isa (A, "uint16")) |
6515 | 59 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
|
60 elseif (isa (A, "uint32")) |
6515 | 61 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
|
62 elseif (isa (A, "uint64")) |
6515 | 63 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
|
64 elseif (isa (A, "int8")) |
6515 | 65 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
|
66 elseif (isa (A, "int16")) |
6515 | 67 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
|
68 elseif (isa (A, "int32")) |
6515 | 69 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
|
70 elseif (isa (A, "int64")) |
6515 | 71 amax = 64; |
4950 | 72 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
|
73 error ("bitcmp: invalid class %s", class (A)); |
4950 | 74 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
|
75 bmax = intmax (class (A)); |
4916 | 76 endif |
77 | |
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 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
|
79 C = bitxor (A, bmax); |
6515 | 80 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
|
81 m = double (k); |
6515 | 82 if (any (m < 1) || any (m > amax)) |
12480
139f993936af
Uppercase variables in script error strings.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
83 error ("bitcmp: K must be in the range [1,%d]", amax); |
4916 | 84 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
|
85 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
|
86 C = bitxor (bitand (A, mask), mask); |
4916 | 87 endif |
6515 | 88 endfunction |
4916 | 89 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
90 |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
91 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
92 %! Amax = 53; |
6515 | 93 %! Bmax = bitmax; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
94 %! A = bitshift (Bmax,-2); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
95 %! assert (bitcmp (A,Amax),bitor (bitshift (1,Amax-1), bitshift (1,Amax-2))); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
96 %! assert (bitcmp (A,Amax-1), bitshift (1,Amax-2)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
97 %! assert (bitcmp (A,Amax-2), 0); |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
98 %!test |
17392
e09cd91168d1
Support arguments of class single in bit manipulation functions (bug #34502)
Mike Miller <mtmiller@ieee.org>
parents:
14363
diff
changeset
|
99 %! Amax = 24; |
e09cd91168d1
Support arguments of class single in bit manipulation functions (bug #34502)
Mike Miller <mtmiller@ieee.org>
parents:
14363
diff
changeset
|
100 %! Bmax = bitmax ("single"); |
e09cd91168d1
Support arguments of class single in bit manipulation functions (bug #34502)
Mike Miller <mtmiller@ieee.org>
parents:
14363
diff
changeset
|
101 %! A = bitshift (Bmax,-2); |
e09cd91168d1
Support arguments of class single in bit manipulation functions (bug #34502)
Mike Miller <mtmiller@ieee.org>
parents:
14363
diff
changeset
|
102 %! assert (bitcmp (A,Amax),bitor (bitshift (single (1),Amax-1), bitshift (single (1),Amax-2))); |
e09cd91168d1
Support arguments of class single in bit manipulation functions (bug #34502)
Mike Miller <mtmiller@ieee.org>
parents:
14363
diff
changeset
|
103 %! assert (bitcmp (A,Amax-1), bitshift (single (1),Amax-2)); |
e09cd91168d1
Support arguments of class single in bit manipulation functions (bug #34502)
Mike Miller <mtmiller@ieee.org>
parents:
14363
diff
changeset
|
104 %! assert (bitcmp (A,Amax-2), single (0)); |
e09cd91168d1
Support arguments of class single in bit manipulation functions (bug #34502)
Mike Miller <mtmiller@ieee.org>
parents:
14363
diff
changeset
|
105 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
106 %! Amax = 8; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
107 %! Bmax = intmax ("uint8"); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
108 %! A = bitshift (Bmax,-2); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
109 %! assert (bitcmp (A,Amax),bitor (bitshift (uint8 (1),Amax-1), bitshift (uint8 (1),Amax-2))); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
110 %! assert (bitcmp (A,Amax-1), bitshift (uint8 (1),Amax-2)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
111 %! assert (bitcmp (A,Amax-2), uint8 (0)); |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
112 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
113 %! Amax = 16; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
114 %! Bmax = intmax ("uint16"); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
115 %! A = bitshift (Bmax,-2); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
116 %! assert (bitcmp (A,Amax),bitor (bitshift (uint16 (1),Amax-1), bitshift (uint16 (1),Amax-2))); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
117 %! assert (bitcmp (A,Amax-1), bitshift (uint16 (1),Amax-2)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
118 %! assert (bitcmp (A,Amax-2), uint16 (0)); |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
119 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
120 %! Amax = 32; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
121 %! Bmax = intmax ("uint32"); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
122 %! A = bitshift (Bmax,-2); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
123 %! assert (bitcmp (A,Amax),bitor (bitshift (uint32 (1),Amax-1), bitshift (uint32 (1),Amax-2))); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
124 %! assert (bitcmp (A,Amax-1), bitshift (uint32 (1),Amax-2)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
125 %! assert (bitcmp (A,Amax-2), uint32 (0)); |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
126 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
127 %! Amax = 64; |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
128 %! Bmax = intmax ("uint64"); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
129 %! A = bitshift (Bmax,-2); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
130 %! assert (bitcmp (A,Amax),bitor (bitshift (uint64 (1),Amax-1), bitshift (uint64 (1),Amax-2))); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
131 %! assert (bitcmp (A,Amax-1), bitshift (uint64 (1),Amax-2)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
132 %! assert (bitcmp (A,Amax-2), uint64 (0)); |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10635
diff
changeset
|
133 |