4916
|
1 ## Copyright (C) 2004 David BAteman |
|
2 ## |
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2 of the License, or |
|
6 ## (at your option) any later version. |
|
7 ## |
|
8 ## This program is distributed in the hope that it will be useful, |
|
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 ## GNU General Public License for more details. |
|
12 ## |
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this program; if not, write to the Free Software |
5307
|
15 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
16 ## 02110-1301 USA |
4916
|
17 |
|
18 ## -*- texinfo -*- |
4920
|
19 ## @deftypefn {Function File} {@var{x} =} bitset (@var{a}, @var{n}) |
|
20 ## @deftypefnx {Function File} {@var{x} =} bitset (@var{a}, @var{n}, @var{v}) |
|
21 ## Set or reset bit(s) @var{n} of unsigned integers in @var{a}. |
4916
|
22 ## @var{v} = 0 resets and @var{v} = 1 sets the bits. |
|
23 ## The lowest significant bit is: @var{n} = 1 |
|
24 ## |
|
25 ## @example |
4920
|
26 ## dec2bin (bitset (10, 1)) |
4916
|
27 ## @result{} 1011 |
|
28 ## @end example |
5053
|
29 ## @end deftypefn |
4916
|
30 ## |
4920
|
31 ## @seealso{bitand, bitor, bitxor, bitget, bitcmp, bitshift, bitmax} |
4916
|
32 |
|
33 ## Liberally based of the version by Kai Habel from octave-forge |
|
34 |
|
35 function X = bitset (A, n, value) |
4920
|
36 |
4916
|
37 if (nargin < 2 || nargin > 3) |
|
38 usage ("bitset (A, n, v)"); |
|
39 endif |
|
40 |
|
41 if (nargin == 2) |
|
42 value = 1; |
|
43 endif |
|
44 |
4950
|
45 if (isa (A, "double")) |
4916
|
46 Bmax = bitmax; |
|
47 Amax = log2 (Bmax) + 1; |
4950
|
48 _conv = @double; |
4916
|
49 else |
4950
|
50 if (isa (A, "uint8")) |
|
51 Amax = 8; |
|
52 _conv = @uint8; |
|
53 elseif (isa (A, "uint16")) |
|
54 Amax = 16; |
|
55 _conv = @uint16; |
|
56 elseif (isa (A, "uint32")) |
|
57 Amax = 32; |
|
58 _conv = @uint32; |
|
59 elseif (isa (A, "uint64")) |
|
60 Amax = 64; |
|
61 _conv = @uint64; |
|
62 elseif (isa (A, "int8")) |
|
63 Amax = 8; |
|
64 _conv = @int8; |
|
65 elseif (isa (A, "int16")) |
|
66 Amax = 16; |
|
67 _conv = @int16; |
|
68 elseif (isa (A, "int32")) |
|
69 Amax = 32; |
|
70 _conv = @int32; |
|
71 elseif (isa (A, "int64")) |
|
72 Amax = 64; |
|
73 _conv = @int64; |
|
74 else |
|
75 error ("invalid class %s", class (A)); |
|
76 endif |
|
77 Bmax = intmax (class (A)); |
4916
|
78 endif |
|
79 |
4950
|
80 m = double (n(:)); |
|
81 if (any (m < 1) || any (m > Amax)) |
4916
|
82 error ("n must be in the range [1,%d]", Amax); |
|
83 endif |
|
84 |
4950
|
85 mask = bitshift (_conv (1), uint8 (n) - uint8 (1)); |
|
86 X = bitxor (A, bitand (A, mask)); |
4916
|
87 |
|
88 if (value) |
4950
|
89 X = bitor (A, mask); |
4916
|
90 endif |
|
91 |
|
92 endfunction |