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