Mercurial > hg > octave-nkf
annotate scripts/general/bitset.m @ 14570:d07d96e53612 stable
seconds after the minute can be 0-60, not 0-61
* system.txi (Timing Utilities): Correct possible values for number of
seconds in time structures. From Rafael Arndt <rafaelarndt@gmail.com>.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 17 Apr 2012 14:42:49 -0400 |
parents | 4d917a6a858b |
children | f3d52523cde1 |
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 |
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:
10793
diff
changeset
|
20 ## @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
|
21 ## @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
|
22 ## 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
|
23 ## @var{val} = 0 resets and @var{val} = 1 sets the bits. |
4916 | 24 ## The lowest significant bit is: @var{n} = 1 |
25 ## | |
26 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
27 ## @group |
4920 | 28 ## 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
|
29 ## @result{} 1011 |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
30 ## @end group |
4916 | 31 ## @end example |
5642 | 32 ## @seealso{bitand, bitor, bitxor, bitget, bitcmp, bitshift, bitmax} |
5053 | 33 ## @end deftypefn |
4916 | 34 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
35 ## Liberally based on the version by Kai Habel from octave-forge |
4916 | 36 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
37 function C = bitset (A, n, val) |
4920 | 38 |
4916 | 39 if (nargin < 2 || nargin > 3) |
6046 | 40 print_usage (); |
4916 | 41 endif |
42 | |
43 if (nargin == 2) | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
44 val = 1; |
4916 | 45 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
46 |
4950 | 47 if (isa (A, "double")) |
4916 | 48 Bmax = bitmax; |
49 Amax = log2 (Bmax) + 1; | |
4950 | 50 _conv = @double; |
4916 | 51 else |
4950 | 52 if (isa (A, "uint8")) |
53 Amax = 8; | |
54 _conv = @uint8; | |
55 elseif (isa (A, "uint16")) | |
56 Amax = 16; | |
57 _conv = @uint16; | |
58 elseif (isa (A, "uint32")) | |
59 Amax = 32; | |
60 _conv = @uint32; | |
61 elseif (isa (A, "uint64")) | |
62 Amax = 64; | |
63 _conv = @uint64; | |
64 elseif (isa (A, "int8")) | |
65 Amax = 8; | |
66 _conv = @int8; | |
67 elseif (isa (A, "int16")) | |
68 Amax = 16; | |
69 _conv = @int16; | |
70 elseif (isa (A, "int32")) | |
71 Amax = 32; | |
72 _conv = @int32; | |
73 elseif (isa (A, "int64")) | |
74 Amax = 64; | |
75 _conv = @int64; | |
76 else | |
10635
d1978e7364ad
Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
77 error ("bitset: invalid class %s", class (A)); |
4950 | 78 endif |
79 Bmax = intmax (class (A)); | |
4916 | 80 endif |
81 | |
4950 | 82 m = double (n(:)); |
83 if (any (m < 1) || any (m > Amax)) | |
12480
139f993936af
Uppercase variables in script error strings.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
84 error ("bitset: N must be in the range [1,%d]", Amax); |
4916 | 85 endif |
86 | |
4950 | 87 mask = bitshift (_conv (1), uint8 (n) - uint8 (1)); |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
88 C = bitxor (A, bitand (A, mask)); |
4916 | 89 |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
90 if (val) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
91 C = bitor (A, mask); |
4916 | 92 endif |
93 | |
94 endfunction | |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
95 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
96 %!error bitset (1); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
97 %!error bitset (1, 2, 3, 4); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
98 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
99 %!test |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
100 %! 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
|
101 %! pfx = {"", "u"}; |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
102 %! for i = 1:2 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
103 %! 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
|
104 %! 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
|
105 %! 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
|
106 %! endfor |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
107 %! endfor |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
108 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
109 %!error bitset (0, 0); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
110 %!error bitset (0, 55); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
111 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
112 %!error bitset (int8 (0), 9); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
113 %!error bitset (uint8 (0), 9); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
114 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
115 %!error bitset (int16 (0), 17); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
116 %!error bitset (uint16 (0), 17); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
117 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
118 %!error bitset (int32 (0), 33); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
119 %!error bitset (uint32 (0), 33); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
120 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
121 %!error bitset (int64 (0), 65); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12480
diff
changeset
|
122 %!error bitset (uint64 (0), 65); |