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 |
|
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
16 |
|
17 ## -*- texinfo -*- |
4920
|
18 ## @deftypefn {Function File} {@var{x} =} bitset (@var{a}, @var{n}) |
|
19 ## @deftypefnx {Function File} {@var{x} =} bitset (@var{a}, @var{n}, @var{v}) |
|
20 ## Set or reset bit(s) @var{n} of unsigned integers in @var{a}. |
4916
|
21 ## @var{v} = 0 resets and @var{v} = 1 sets the bits. |
|
22 ## The lowest significant bit is: @var{n} = 1 |
|
23 ## |
|
24 ## @example |
4920
|
25 ## dec2bin (bitset (10, 1)) |
4916
|
26 ## @result{} 1011 |
|
27 ## @end example |
5053
|
28 ## @end deftypefn |
4916
|
29 ## |
4920
|
30 ## @seealso{bitand, bitor, bitxor, bitget, bitcmp, bitshift, bitmax} |
4916
|
31 |
|
32 ## Liberally based of the version by Kai Habel from octave-forge |
|
33 |
|
34 function X = bitset (A, n, value) |
4920
|
35 |
4916
|
36 if (nargin < 2 || nargin > 3) |
|
37 usage ("bitset (A, n, v)"); |
|
38 endif |
|
39 |
|
40 if (nargin == 2) |
|
41 value = 1; |
|
42 endif |
|
43 |
4950
|
44 if (isa (A, "double")) |
4916
|
45 Bmax = bitmax; |
|
46 Amax = log2 (Bmax) + 1; |
4950
|
47 _conv = @double; |
4916
|
48 else |
4950
|
49 if (isa (A, "uint8")) |
|
50 Amax = 8; |
|
51 _conv = @uint8; |
|
52 elseif (isa (A, "uint16")) |
|
53 Amax = 16; |
|
54 _conv = @uint16; |
|
55 elseif (isa (A, "uint32")) |
|
56 Amax = 32; |
|
57 _conv = @uint32; |
|
58 elseif (isa (A, "uint64")) |
|
59 Amax = 64; |
|
60 _conv = @uint64; |
|
61 elseif (isa (A, "int8")) |
|
62 Amax = 8; |
|
63 _conv = @int8; |
|
64 elseif (isa (A, "int16")) |
|
65 Amax = 16; |
|
66 _conv = @int16; |
|
67 elseif (isa (A, "int32")) |
|
68 Amax = 32; |
|
69 _conv = @int32; |
|
70 elseif (isa (A, "int64")) |
|
71 Amax = 64; |
|
72 _conv = @int64; |
|
73 else |
|
74 error ("invalid class %s", class (A)); |
|
75 endif |
|
76 Bmax = intmax (class (A)); |
4916
|
77 endif |
|
78 |
4950
|
79 m = double (n(:)); |
|
80 if (any (m < 1) || any (m > Amax)) |
4916
|
81 error ("n must be in the range [1,%d]", Amax); |
|
82 endif |
|
83 |
4950
|
84 mask = bitshift (_conv (1), uint8 (n) - uint8 (1)); |
|
85 X = bitxor (A, bitand (A, mask)); |
4916
|
86 |
|
87 if (value) |
4950
|
88 X = bitor (A, mask); |
4916
|
89 endif |
|
90 |
|
91 endfunction |