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 |
4920
|
37 cname = class (A); |
4916
|
38 if (strcmp (cname, "double")) |
|
39 Amax = log2 (bitmax) + 1; |
4920
|
40 elseif strcmp ("uint", substr (cname, 1, 4)) |
|
41 Amax = eval ([cname, " (log2 (double (intmax (cname))) + 1);"]); |
4916
|
42 else |
4920
|
43 Amax = eval ([cname, " (log2 (double (intmax (cname))) + 2);"]); |
4916
|
44 endif |
|
45 |
4920
|
46 Aone = eval ([ cname, "(1);"]); |
|
47 m = eval ([cname, " (n(:));"]); |
|
48 if (any (m < Aone) || any (m > Amax)) |
4916
|
49 error ("n must be in the range [1,%d]", Amax); |
|
50 endif |
|
51 |
|
52 X = eval (["bitand (A, ", cname, " (2) .^ (", cname, " (n) - Aone)) != ", cname, "(0);"]); |
|
53 |
|
54 endfunction |