1 ## Copyright (C) 2004-2012 David Bateman
3 ## This file is part of Octave.
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.
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.
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/>.
20 ## @deftypefn {Function File} {@var{c} =} bitget (@var{A}, @var{n})
21 ## Return the status of bit(s) @var{n} of unsigned integers in @var{A}
22 ## the lowest significant bit is @var{n} = 1.
26 ## bitget (100, 8:-1:1)
27 ## @result{} 0 1 1 0 0 1 0 0
30 ## @seealso{bitand, bitor, bitxor, bitset, bitcmp, bitshift, bitmax}
33 ## Liberally based of the version by Kai Habel from octave-forge
35 function C = bitget (A, n)
41 if (isa (A, "double"))
42 Amax = log2 (bitmax) + 1;
48 elseif (isa (A, "uint16"))
51 elseif (isa (A, "uint32"))
54 elseif (isa (A, "uint64"))
57 elseif (isa (A, "int8"))
60 elseif (isa (A, "int16"))
63 elseif (isa (A, "int32"))
66 elseif (isa (A, "int64"))
70 error ("bitget: invalid class %s", class (A));
75 if (any (m < 1) || any (m > Amax))
76 error ("bitget: N must be in the range [1,%d]", Amax);
79 C = bitand (A, bitshift (_conv (1), uint8 (n) - uint8 (1))) != _conv (0);
84 %!error bitget (1, 2, 3);
87 %! assert (bitget ([4, 14], [3, 3]), logical ([1, 1]));
90 %! for prec = [8, 16, 32, 64]
91 %! fcn = str2func (sprintf ("%sint%d", pfx{i}, prec));
92 %! assert (bitget (fcn ([4, 14]), [3, 3]), logical ([1, 1]));
96 %!error bitget (0, 0);
97 %!error bitget (0, 55);
99 %!error bitget (int8 (0), 9);
100 %!error bitget (uint8 (0), 9);
102 %!error bitget (int16 (0), 17);
103 %!error bitget (uint16 (0), 17);
105 %!error bitget (int32 (0), 33);
106 %!error bitget (uint32 (0), 33);
108 %!error bitget (int64 (0), 65);
109 %!error bitget (uint64 (0), 65);