0
|
1 ## Copyright (C) 2000 Kai Habel |
|
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 -*- |
|
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 ## sets or resets bit(s) @var{N} of unsigned integers in @var{A}. |
|
21 ## @var{v} = 0 resets and @var{v} = 1 sets the bits. |
|
22 ## The lowest significant bit is: @var{n} = 1 |
|
23 ## |
|
24 ## @example |
|
25 ## dec2bin (bitset(10,1)) |
|
26 ## @result{} 1011 |
|
27 ## @seealso{bitand,bitor,bitxor,bitget,bitcmp,bitshift,bitmax} |
|
28 ## @end deftypefn |
|
29 |
|
30 ## Author: Kai Habel <kai.habel@gmx.de> |
|
31 |
|
32 function X = bitset (A, n, value) |
|
33 |
|
34 if (nargin < 2 || nargin > 3) |
|
35 usage ("bitset (A, n, v)"); |
|
36 endif |
|
37 |
|
38 if (nargin == 2) |
|
39 value = 1; |
|
40 endif |
|
41 |
|
42 if (n < 1 || n > (log2(bitmax) + 1) ) |
|
43 msg = sprintf ("n must be in range [1,%d]",round(log2(bitmax)+1)); |
|
44 error (msg); |
|
45 endif |
|
46 |
|
47 if (!is_matrix (A) || is_complex (A)) |
|
48 error ("first argument must be a real value"); |
|
49 else |
|
50 X = bitand (pow2 (n - 1), value); |
|
51 Y = bitand (A, bitmax - pow2 (n - 1)); |
|
52 X = bitor (X, Y); |
|
53 endif |
|
54 |
|
55 endfunction |