annotate scripts/general/bitset.m @ 11523:fd0a3ac60b0e

update copyright notices
author John W. Eaton <jwe@octave.org>
date Fri, 14 Jan 2011 05:47:45 -0500
parents c776f063fefe
children c792872f8942
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11523
fd0a3ac60b0e update copyright notices
John W. Eaton <jwe@octave.org>
parents: 11469
diff changeset
1 ## Copyright (C) 2004-2011 David Bateman
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
2 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
3 ## This file is part of Octave.
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
4 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
6 ## under the terms of the GNU General Public License as published by
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
8 ## your option) any later version.
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
9 ##
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
13 ## General Public License for more details.
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
14 ##
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
17 ## <http://www.gnu.org/licenses/>.
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
18
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
20 ## @deftypefn {Function File} {@var{C} =} bitset (@var{A}, @var{n})
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
21 ## @deftypefnx {Function File} {@var{C} =} bitset (@var{A}, @var{n}, @var{val})
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
22 ## Set or reset bit(s) @var{n} of unsigned integers in @var{A}.
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
23 ## @var{val} = 0 resets and @var{val} = 1 sets the bits.
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
24 ## The lowest significant bit is: @var{n} = 1
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
25 ##
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
26 ## @example
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 7017
diff changeset
27 ## @group
4920
b22a7a1db0d5 [project @ 2004-07-27 18:15:25 by jwe]
jwe
parents: 4916
diff changeset
28 ## dec2bin (bitset (10, 1))
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
29 ## @result{} 1011
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 7017
diff changeset
30 ## @end group
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
31 ## @end example
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5307
diff changeset
32 ## @seealso{bitand, bitor, bitxor, bitget, bitcmp, bitshift, bitmax}
5053
c08cb1098afc [project @ 2004-10-19 23:10:54 by jwe]
jwe
parents: 4950
diff changeset
33 ## @end deftypefn
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
34
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
35 ## Liberally based on the version by Kai Habel from octave-forge
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
36
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
37 function C = bitset (A, n, val)
4920
b22a7a1db0d5 [project @ 2004-07-27 18:15:25 by jwe]
jwe
parents: 4916
diff changeset
38
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
39 if (nargin < 2 || nargin > 3)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
40 print_usage ();
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
41 endif
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
42
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
43 if (nargin == 2)
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
44 val = 1;
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
45 endif
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
46
4950
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
47 if (isa (A, "double"))
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
48 Bmax = bitmax;
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
49 Amax = log2 (Bmax) + 1;
4950
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
50 _conv = @double;
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
51 else
4950
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
52 if (isa (A, "uint8"))
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
53 Amax = 8;
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
54 _conv = @uint8;
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
55 elseif (isa (A, "uint16"))
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
56 Amax = 16;
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
57 _conv = @uint16;
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
58 elseif (isa (A, "uint32"))
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
59 Amax = 32;
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
60 _conv = @uint32;
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
61 elseif (isa (A, "uint64"))
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
62 Amax = 64;
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
63 _conv = @uint64;
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
64 elseif (isa (A, "int8"))
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
65 Amax = 8;
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
66 _conv = @int8;
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
67 elseif (isa (A, "int16"))
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
68 Amax = 16;
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
69 _conv = @int16;
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
70 elseif (isa (A, "int32"))
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
71 Amax = 32;
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
72 _conv = @int32;
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
73 elseif (isa (A, "int64"))
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
74 Amax = 64;
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
75 _conv = @int64;
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
76 else
10635
d1978e7364ad Print name of function in error() string messages.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
77 error ("bitset: invalid class %s", class (A));
4950
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
78 endif
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
79 Bmax = intmax (class (A));
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
80 endif
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
81
4950
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
82 m = double (n(:));
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
83 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
84 error ("bitset: n must be in the range [1,%d]", Amax);
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
85 endif
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
86
4950
0896307b1b48 [project @ 2004-08-31 21:26:35 by jwe]
jwe
parents: 4920
diff changeset
87 mask = bitshift (_conv (1), uint8 (n) - uint8 (1));
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
88 C = bitxor (A, bitand (A, mask));
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
89
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
90 if (val)
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
91 C = bitor (A, mask);
4916
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
92 endif
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
93
c45f14873b5e [project @ 2004-07-26 14:35:12 by jwe]
jwe
parents:
diff changeset
94 endfunction