Mercurial > hg > octave-lyh
annotate scripts/general/bitget.m @ 11188:4cb1522e4d0f
Use function handle as input to cellfun,
rather than quoted function name or anonymous function wrapper.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 03 Nov 2010 17:20:56 -0700 |
parents | d1978e7364ad |
children | c776f063fefe |
rev | line source |
---|---|
9245 | 1 ## Copyright (C) 2004, 2005, 2006, 2007, 2009 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 | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
25 ## @group |
4920 | 26 ## bitget (100, 8:-1:1) |
4916 | 27 ## @result{} 0 1 1 0 0 1 0 0 |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
28 ## @end group |
4916 | 29 ## @end example |
5642 | 30 ## @seealso{bitand, bitor, bitxor, bitset, bitcmp, bitshift, bitmax} |
5053 | 31 ## @end deftypefn |
4916 | 32 |
33 ## Liberally based of the version by Kai Habel from octave-forge | |
34 | |
35 function X = bitget (A, n) | |
4920 | 36 |
4916 | 37 if (nargin != 2) |
6046 | 38 print_usage (); |
4916 | 39 endif |
40 | |
4950 | 41 if (isa (A, "double")) |
5003 | 42 Amax = log2 (bitmax) + 1; |
4950 | 43 _conv = @double; |
4916 | 44 else |
4950 | 45 if (isa (A, "uint8")) |
46 Amax = 8; | |
47 _conv = @uint8; | |
48 elseif (isa (A, "uint16")) | |
49 Amax = 16; | |
50 _conv = @uint16; | |
51 elseif (isa (A, "uint32")) | |
52 Amax = 32; | |
53 _conv = @uint32; | |
54 elseif (isa (A, "uint64")) | |
55 Amax = 64; | |
56 _conv = @uint64; | |
57 elseif (isa (A, "int8")) | |
58 Amax = 8; | |
59 _conv = @int8; | |
60 elseif (isa (A, "int16")) | |
61 Amax = 16; | |
62 _conv = @int16; | |
63 elseif (isa (A, "int32")) | |
64 Amax = 32; | |
65 _conv = @int32; | |
66 elseif (isa (A, "int64")) | |
67 Amax = 64; | |
68 _conv = @int64; | |
69 else | |
10635
d1978e7364ad
Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
70 error ("bitget: invalid class %s", class (A)); |
4950 | 71 endif |
4916 | 72 endif |
73 | |
4950 | 74 m = double (n(:)); |
75 if (any (m < 1) || any (m > Amax)) | |
10635
d1978e7364ad
Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
76 error ("bitget: n must be in the range [1,%d]", Amax); |
4916 | 77 endif |
78 | |
4950 | 79 X = bitand (A, bitshift (_conv (1), uint8 (n) - uint8 (1))) != _conv (0); |
4916 | 80 endfunction |