7017
|
1 ## Copyright (C) 2004, 2005, 2006, 2007 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 -*- |
4920
|
20 ## @deftypefn {Function File} {@var{x} =} bitset (@var{a}, @var{n}) |
|
21 ## @deftypefnx {Function File} {@var{x} =} bitset (@var{a}, @var{n}, @var{v}) |
|
22 ## Set or reset bit(s) @var{n} of unsigned integers in @var{a}. |
4916
|
23 ## @var{v} = 0 resets and @var{v} = 1 sets the bits. |
|
24 ## The lowest significant bit is: @var{n} = 1 |
|
25 ## |
|
26 ## @example |
4920
|
27 ## dec2bin (bitset (10, 1)) |
4916
|
28 ## @result{} 1011 |
|
29 ## @end example |
5642
|
30 ## @seealso{bitand, bitor, bitxor, bitget, bitcmp, bitshift, bitmax} |
5053
|
31 ## @end deftypefn |
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) |
6046
|
38 print_usage (); |
4916
|
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 |