changelog shortlog tags changeset files revisions annotate raw

scripts/general/bitget.m

changeset 10289: 4b124317dc38
parent:1bf0ce0930be
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (40 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 2004, 2005, 2006, 2007, 2009 David Bateman
2##
3## This file is part of Octave.
4##
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.
14##
15## You should have received a copy of the GNU General Public License
16## along with Octave; see the file COPYING. If not, see
17## <http://www.gnu.org/licenses/>.
18
19## -*- texinfo -*-
20## @deftypefn {Function File} {@var{X} =} bitget (@var{a},@var{n})
21## Return the status of bit(s) @var{n} of unsigned integers in @var{a}
22## the lowest significant bit is @var{n} = 1.
23##
24## @example
25## @group
26## bitget (100, 8:-1:1)
27## @result{} 0 1 1 0 0 1 0 0
28## @end group
29## @end example
30## @seealso{bitand, bitor, bitxor, bitset, bitcmp, bitshift, bitmax}
31## @end deftypefn
32
33## Liberally based of the version by Kai Habel from octave-forge
34
35function X = bitget (A, n)
36
37 if (nargin != 2)
38 print_usage ();
39 endif
40
41 if (isa (A, "double"))
42 Amax = log2 (bitmax) + 1;
43 _conv = @double;
44 else
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
70 error ("invalid class %s", class (A));
71 endif
72 endif
73
74 m = double (n(:));
75 if (any (m < 1) || any (m > Amax))
76 error ("n must be in the range [1,%d]", Amax);
77 endif
78
79 X = bitand (A, bitshift (_conv (1), uint8 (n) - uint8 (1))) != _conv (0);
80endfunction