]> Creatis software - CreaPhase.git/blob - octave_packages/m/general/bitget.m
update packages
[CreaPhase.git] / octave_packages / m / general / bitget.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} =} 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.
23 ##
24 ## @example
25 ## @group
26 ## bitget (100, 8:-1:1)
27 ## @result{} 0  1  1  0  0  1  0  0
28 ## @end group
29 ## @end example
30 ## @seealso{bitand, bitor, bitxor, bitset, bitcmp, bitshift, bitmax}
31 ## @end deftypefn
32
33 ## Liberally based of the version by Kai Habel from octave-forge
34
35 function C = bitget (A, n)
36
37   if (nargin != 2)
38     print_usage ();
39   endif
40
41   if (isa (A, "double"))
42     Amax = log2 (bitmax) + 1;
43     _conv = @double;
44   else
45     if (isa (A, "uint8"))
46       Amax = 8;
47       _conv = @uint8;
48     elseif (isa (A, "uint16"))
49       Amax = 16;
50       _conv = @uint16;
51     elseif (isa (A, "uint32"))
52       Amax = 32;
53       _conv = @uint32;
54     elseif (isa (A, "uint64"))
55       Amax = 64;
56       _conv = @uint64;
57     elseif (isa (A, "int8"))
58       Amax = 8;
59       _conv = @int8;
60     elseif (isa (A, "int16"))
61       Amax = 16;
62       _conv = @int16;
63     elseif (isa (A, "int32"))
64       Amax = 32;
65       _conv = @int32;
66     elseif (isa (A, "int64"))
67       Amax = 64;
68       _conv = @int64;
69     else
70       error ("bitget: invalid class %s", class (A));
71     endif
72   endif
73
74   m = double (n(:));
75   if (any (m < 1) || any (m > Amax))
76     error ("bitget: N must be in the range [1,%d]", Amax);
77   endif
78
79   C = bitand (A, bitshift (_conv (1), uint8 (n) - uint8 (1))) != _conv (0);
80
81 endfunction
82
83 %!error bitget (1);
84 %!error bitget (1, 2, 3);
85
86 %!test
87 %! assert (bitget ([4, 14], [3, 3]), logical ([1, 1]));
88 %! pfx = {"", "u"};
89 %! for i = 1:2
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]));
93 %!   endfor
94 %! endfor
95
96 %!error bitget (0, 0);
97 %!error bitget (0, 55);
98
99 %!error bitget (int8 (0), 9);
100 %!error bitget (uint8 (0), 9);
101
102 %!error bitget (int16 (0), 17);
103 %!error bitget (uint16 (0), 17);
104
105 %!error bitget (int32 (0), 33);
106 %!error bitget (uint32 (0), 33);
107
108 %!error bitget (int64 (0), 65);
109 %!error bitget (uint64 (0), 65);