7017
|
1 ## Copyright (C) 2004, 2005, 2006, 2007 David BAteman |
4916
|
2 ## |
7016
|
3 ## This file is part of Octave. |
4916
|
4 ## |
7016
|
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. |
4916
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
4916
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {@var{X} =} bitget (@var{a},@var{n}) |
4920
|
21 ## Return the status of bit(s) @var{n} of unsigned integers in @var{a} |
4916
|
22 ## the lowest significant bit is @var{n} = 1. |
|
23 ## |
|
24 ## @example |
4920
|
25 ## bitget (100, 8:-1:1) |
4916
|
26 ## @result{} 0 1 1 0 0 1 0 0 |
|
27 ## @end example |
5642
|
28 ## @seealso{bitand, bitor, bitxor, bitset, bitcmp, bitshift, bitmax} |
5053
|
29 ## @end deftypefn |
4916
|
30 |
|
31 ## Liberally based of the version by Kai Habel from octave-forge |
|
32 |
|
33 function X = bitget (A, n) |
4920
|
34 |
4916
|
35 if (nargin != 2) |
6046
|
36 print_usage (); |
4916
|
37 endif |
|
38 |
4950
|
39 if (isa (A, "double")) |
5003
|
40 Amax = log2 (bitmax) + 1; |
4950
|
41 _conv = @double; |
4916
|
42 else |
4950
|
43 if (isa (A, "uint8")) |
|
44 Amax = 8; |
|
45 _conv = @uint8; |
|
46 elseif (isa (A, "uint16")) |
|
47 Amax = 16; |
|
48 _conv = @uint16; |
|
49 elseif (isa (A, "uint32")) |
|
50 Amax = 32; |
|
51 _conv = @uint32; |
|
52 elseif (isa (A, "uint64")) |
|
53 Amax = 64; |
|
54 _conv = @uint64; |
|
55 elseif (isa (A, "int8")) |
|
56 Amax = 8; |
|
57 _conv = @int8; |
|
58 elseif (isa (A, "int16")) |
|
59 Amax = 16; |
|
60 _conv = @int16; |
|
61 elseif (isa (A, "int32")) |
|
62 Amax = 32; |
|
63 _conv = @int32; |
|
64 elseif (isa (A, "int64")) |
|
65 Amax = 64; |
|
66 _conv = @int64; |
|
67 else |
|
68 error ("invalid class %s", class (A)); |
|
69 endif |
4916
|
70 endif |
|
71 |
4950
|
72 m = double (n(:)); |
|
73 if (any (m < 1) || any (m > Amax)) |
4916
|
74 error ("n must be in the range [1,%d]", Amax); |
|
75 endif |
|
76 |
4950
|
77 X = bitand (A, bitshift (_conv (1), uint8 (n) - uint8 (1))) != _conv (0); |
4916
|
78 endfunction |