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}) |
|
19 ## returns the status of bit(s) @var{n} of unsigned integers in @var{a} |
|
20 ## the lowest significant bit is @var{n} = 1. |
|
21 ## |
|
22 ## @example |
|
23 ## bitget(100,8:-1:1) |
|
24 ## @result{} 0 1 1 0 0 1 0 0 |
|
25 ## @end example |
|
26 ## @seealso{bitand,bitor,bitxor,bitset,bitcmp,bitshift,bitmax} |
|
27 ## @end deftypefn |
|
28 |
|
29 ## Liberally based of the version by Kai Habel from octave-forge |
|
30 |
|
31 function X = bitget (A, n) |
|
32 if (nargin != 2) |
|
33 usage ("bitget(A,n)"); |
|
34 endif |
|
35 |
|
36 cname = class(A); |
|
37 if (strcmp (cname, "double")) |
|
38 Amax = log2 (bitmax) + 1; |
|
39 elseif strcmp("uint",substr(cname,1,4)) |
|
40 Amax = eval([cname, " (log2 (double (intmax (cname))) + 1);"]); |
|
41 else |
|
42 Amax = eval([cname, " (log2 (double (intmax (cname))) + 2);"]); |
|
43 endif |
|
44 |
|
45 Aone = eval([ cname, "(1);"]); |
|
46 m = eval([cname, " (n(:));"]); |
|
47 if (any(m < Aone) || any( m > Amax)) |
|
48 error ("n must be in the range [1,%d]", Amax); |
|
49 endif |
|
50 |
|
51 X = eval (["bitand (A, ", cname, " (2) .^ (", cname, " (n) - Aone)) != ", cname, "(0);"]); |
|
52 |
|
53 endfunction |