4916
|
1 ## Copyright (C) 2004 David BAteman |
|
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} =} bitget (@var{a},@var{n}) |
4920
|
19 ## Return the status of bit(s) @var{n} of unsigned integers in @var{a} |
4916
|
20 ## the lowest significant bit is @var{n} = 1. |
|
21 ## |
|
22 ## @example |
4920
|
23 ## bitget (100, 8:-1:1) |
4916
|
24 ## @result{} 0 1 1 0 0 1 0 0 |
|
25 ## @end example |
4920
|
26 ## @seealso{bitand, bitor, bitxor, bitset, bitcmp, bitshift, bitmax} |
4916
|
27 ## @end deftypefn |
|
28 |
|
29 ## Liberally based of the version by Kai Habel from octave-forge |
|
30 |
|
31 function X = bitget (A, n) |
4920
|
32 |
4916
|
33 if (nargin != 2) |
4920
|
34 usage ("bitget (A, n)"); |
4916
|
35 endif |
|
36 |
4950
|
37 if (isa (A, "double")) |
5003
|
38 Amax = log2 (bitmax) + 1; |
4950
|
39 _conv = @double; |
4916
|
40 else |
4950
|
41 if (isa (A, "uint8")) |
|
42 Amax = 8; |
|
43 _conv = @uint8; |
|
44 elseif (isa (A, "uint16")) |
|
45 Amax = 16; |
|
46 _conv = @uint16; |
|
47 elseif (isa (A, "uint32")) |
|
48 Amax = 32; |
|
49 _conv = @uint32; |
|
50 elseif (isa (A, "uint64")) |
|
51 Amax = 64; |
|
52 _conv = @uint64; |
|
53 elseif (isa (A, "int8")) |
|
54 Amax = 8; |
|
55 _conv = @int8; |
|
56 elseif (isa (A, "int16")) |
|
57 Amax = 16; |
|
58 _conv = @int16; |
|
59 elseif (isa (A, "int32")) |
|
60 Amax = 32; |
|
61 _conv = @int32; |
|
62 elseif (isa (A, "int64")) |
|
63 Amax = 64; |
|
64 _conv = @int64; |
|
65 else |
|
66 error ("invalid class %s", class (A)); |
|
67 endif |
4916
|
68 endif |
|
69 |
4950
|
70 m = double (n(:)); |
|
71 if (any (m < 1) || any (m > Amax)) |
4916
|
72 error ("n must be in the range [1,%d]", Amax); |
|
73 endif |
|
74 |
4950
|
75 X = bitand (A, bitshift (_conv (1), uint8 (n) - uint8 (1))) != _conv (0); |
4916
|
76 endfunction |