annotate scripts/general/bitget.m @ 4916:c45f14873b5e

[project @ 2004-07-26 14:35:12 by jwe]
author jwe
date Mon, 26 Jul 2004 14:37:05 +0000
parents
children b22a7a1db0d5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
1 ## Copyright (C) 2004 David BAteman
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
2 ##
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
3 ## This program is free software; you can redistribute it and/or modify
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
4 ## it under the terms of the GNU General Public License as published by
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
5 ## the Free Software Foundation; either version 2 of the License, or
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
6 ## (at your option) any later version.
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
7 ##
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
8 ## This program is distributed in the hope that it will be useful,
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
11 ## GNU General Public License for more details.
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
12 ##
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
13 ## You should have received a copy of the GNU General Public License
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
14 ## along with this program; if not, write to the Free Software
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
16
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
17 ## -*- texinfo -*-
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
18 ## @deftypefn {Function File} {@var{X} =} bitget (@var{a},@var{n})
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
19 ## returns the status of bit(s) @var{n} of unsigned integers in @var{a}
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
20 ## the lowest significant bit is @var{n} = 1.
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
21 ##
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
22 ## @example
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
23 ## bitget(100,8:-1:1)
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
24 ## @result{} 0 1 1 0 0 1 0 0
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
25 ## @end example
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
26 ## @seealso{bitand,bitor,bitxor,bitset,bitcmp,bitshift,bitmax}
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
27 ## @end deftypefn
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
28
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
29 ## Liberally based of the version by Kai Habel from octave-forge
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
30
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
31 function X = bitget (A, n)
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
32 if (nargin != 2)
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
33 usage ("bitget(A,n)");
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
34 endif
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
35
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
36 cname = class(A);
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
37 if (strcmp (cname, "double"))
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
38 Amax = log2 (bitmax) + 1;
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
39 elseif strcmp("uint",substr(cname,1,4))
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
40 Amax = eval([cname, " (log2 (double (intmax (cname))) + 1);"]);
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
41 else
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
42 Amax = eval([cname, " (log2 (double (intmax (cname))) + 2);"]);
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
43 endif
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
44
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
45 Aone = eval([ cname, "(1);"]);
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
46 m = eval([cname, " (n(:));"]);
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
47 if (any(m < Aone) || any( m > Amax))
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
48 error ("n must be in the range [1,%d]", Amax);
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
49 endif
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
50
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
51 X = eval (["bitand (A, ", cname, " (2) .^ (", cname, " (n) - Aone)) != ", cname, "(0);"]);
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
52
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
53 endfunction