]> Creatis software - CreaPhase.git/blob - octave_packages/m/general/bitset.m
update packages
[CreaPhase.git] / octave_packages / m / general / bitset.m
1 ## Copyright (C) 2004-2012 David Bateman
2 ##
3 ## This file is part of Octave.
4 ##
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.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Function File} {@var{C} =} bitset (@var{A}, @var{n})
21 ## @deftypefnx {Function File} {@var{C} =} bitset (@var{A}, @var{n}, @var{val})
22 ## Set or reset bit(s) @var{n} of unsigned integers in @var{A}.
23 ## @var{val} = 0 resets and @var{val} = 1 sets the bits.
24 ## The lowest significant bit is: @var{n} = 1
25 ##
26 ## @example
27 ## @group
28 ## dec2bin (bitset (10, 1))
29 ##   @result{} 1011
30 ## @end group
31 ## @end example
32 ## @seealso{bitand, bitor, bitxor, bitget, bitcmp, bitshift, bitmax}
33 ## @end deftypefn
34
35 ## Liberally based on the version by Kai Habel from octave-forge
36
37 function C = bitset (A, n, val)
38
39   if (nargin < 2 || nargin > 3)
40     print_usage ();
41   endif
42
43   if (nargin == 2)
44     val = 1;
45   endif
46
47   if (isa (A, "double"))
48     Bmax = bitmax;
49     Amax = log2 (Bmax) + 1;
50     _conv = @double;
51   else
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
77       error ("bitset: invalid class %s", class (A));
78     endif
79     Bmax = intmax (class (A));
80   endif
81
82   m = double (n(:));
83   if (any (m < 1) || any (m > Amax))
84     error ("bitset: N must be in the range [1,%d]", Amax);
85   endif
86
87   mask = bitshift (_conv (1), uint8 (n) - uint8 (1));
88   C = bitxor (A, bitand (A, mask));
89
90   if (val)
91     C = bitor (A, mask);
92   endif
93
94 endfunction
95
96 %!error bitset (1);
97 %!error bitset (1, 2, 3, 4);
98
99 %!test
100 %! assert (bitset ([0, 10], [3, 3]), [4, 14]);
101 %! pfx = {"", "u"};
102 %! for i = 1:2
103 %!   for prec = [8, 16, 32, 64]
104 %!     fcn = str2func (sprintf ("%sint%d", pfx{i}, prec));
105 %!     assert (bitset (fcn ([0, 10]), [3, 3]), fcn ([4, 14]));
106 %!   endfor
107 %! endfor
108
109 %!error bitset (0, 0);
110 %!error bitset (0, 55);
111
112 %!error bitset (int8 (0), 9);
113 %!error bitset (uint8 (0), 9);
114
115 %!error bitset (int16 (0), 17);
116 %!error bitset (uint16 (0), 17);
117
118 %!error bitset (int32 (0), 33);
119 %!error bitset (uint32 (0), 33);
120
121 %!error bitset (int64 (0), 65);
122 %!error bitset (uint64 (0), 65);