Mercurial > octave-nkf
annotate scripts/general/bitset.m @ 14633:056ea51070b8
Rewrite bitset.m (bug #36458)
* bitset.m: Revamp the function. Make it work with arrays and scalars.
No broadcasting yet. Add a test.
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Mon, 14 May 2012 00:46:51 -0400 |
parents | f3d52523cde1 |
children | 7e10eb490b87 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
12795
diff
changeset
|
1 ## Copyright (C) 2004-2012 David Bateman |
14633
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
2 ## Copyright (C) 2012 Jordi GutiƩrrez Hermoso |
4916 | 3 ## |
7016 | 4 ## This file is part of Octave. |
4916 | 5 ## |
7016 | 6 ## Octave is free software; you can redistribute it and/or modify it |
7 ## under the terms of the GNU General Public License as published by | |
8 ## the Free Software Foundation; either version 3 of the License, or (at | |
9 ## your option) any later version. | |
10 ## | |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
4916 | 15 ## |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
4916 | 19 |
20 ## -*- texinfo -*- | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
21 ## @deftypefn {Function File} {@var{C} =} bitset (@var{A}, @var{n}) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
22 ## @deftypefnx {Function File} {@var{C} =} bitset (@var{A}, @var{n}, @var{val}) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
23 ## Set or reset bit(s) @var{n} of unsigned integers in @var{A}. |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
24 ## @var{val} = 0 resets and @var{val} = 1 sets the bits. |
14633
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
25 ## The lowest significant bit is: @var{n} = 1. All variables must be the |
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
26 ## same size or scalars. |
4916 | 27 ## |
28 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
29 ## @group |
4920 | 30 ## dec2bin (bitset (10, 1)) |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
31 ## @result{} 1011 |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
32 ## @end group |
4916 | 33 ## @end example |
5642 | 34 ## @seealso{bitand, bitor, bitxor, bitget, bitcmp, bitshift, bitmax} |
5053 | 35 ## @end deftypefn |
4916 | 36 |
14633
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
37 function A = bitset (A, n, val) |
4920 | 38 |
4916 | 39 if (nargin < 2 || nargin > 3) |
6046 | 40 print_usage (); |
4916 | 41 endif |
42 | |
14633
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
43 sz = size (A); |
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
44 |
4916 | 45 if (nargin == 2) |
14633
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
46 val = true (sz); |
4916 | 47 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
48 |
14633
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
49 cl = class (A); |
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
50 |
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
51 if (isfloat (A) && isreal (A)) |
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
52 Bmax = bitmax (cl); |
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
53 Amax = log2 (Bmax); |
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
54 elseif (isinteger (A)) |
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
55 Bmax = intmax (cl); |
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
56 ## FIXME: Better way to get number of bits than regexping? |
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
57 Amax = str2num (nthargout (4, @regexp, cl, "\d{1,2}"){1}); |
4916 | 58 else |
14633
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
59 error ("bitset: invalid class %s", cl); |
4916 | 60 endif |
61 | |
14633
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
62 if (any ((n < 1)(:)) || any ((n > Amax)(:))) |
12480
139f993936af
Uppercase variables in script error strings.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
63 error ("bitset: N must be in the range [1,%d]", Amax); |
4916 | 64 endif |
65 | |
14633
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
66 mask = bitshift (cast (1, cl), uint8 (n) - uint8 (1)); |
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
67 |
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
68 on = logical (val); |
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
69 off = !on; |
4916 | 70 |
14633
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
71 if (isscalar (mask)) |
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
72 onmask = mask; |
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
73 offmask = mask; |
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
74 else |
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
75 if (! size_equal (A, n)) |
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
76 error ("bitset: N must be scalar or the same size as A"); |
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
77 endif |
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
78 onmask = mask(on); |
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
79 offmask = mask(off); |
4916 | 80 endif |
81 | |
14633
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
82 A(on) = bitor (A(on), onmask); |
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
83 A(off) = bitand (A(off), bitcmp (offmask)); |
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
84 |
4916 | 85 endfunction |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
86 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
87 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
88 %!test |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
89 %! assert (bitset ([0, 10], [3, 3]), [4, 14]); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
90 %! pfx = {"", "u"}; |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
91 %! for i = 1:2 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
92 %! for prec = [8, 16, 32, 64] |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
93 %! fcn = str2func (sprintf ("%sint%d", pfx{i}, prec)); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
94 %! assert (bitset (fcn ([0, 10]), [3, 3]), fcn ([4, 14])); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
95 %! endfor |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
96 %! endfor |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
97 |
14633
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
98 ## Bug #36458 |
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
99 %!assert (bitset(uint8 ([1, 2;3 4]), 1, [0 1; 0 1]), uint8 ([0, 3; 2 5])) |
056ea51070b8
Rewrite bitset.m (bug #36458)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14363
diff
changeset
|
100 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
101 %!error bitset (0, 0) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
102 %!error bitset (0, 55) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
103 %!error bitset (int8 (0), 9) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
104 %!error bitset (uint8 (0), 9) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
105 %!error bitset (int16 (0), 17) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
106 %!error bitset (uint16 (0), 17) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
107 %!error bitset (int32 (0), 33) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
108 %!error bitset (uint32 (0), 33) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
109 %!error bitset (int64 (0), 65) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
110 %!error bitset (uint64 (0), 65) |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
111 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
112 %!error bitset (1) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
113 %!error bitset (1, 2, 3, 4) |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
114 |