# HG changeset patch # User Rik # Date 1648849194 25200 # Node ID 74089676bd9d82ddf7c0914bbc32583dc7b71d9e # Parent af6623656f5313795e62ef11661040e50fcb241b bitset.m: Clean up function. * bitset.m: Rename output variable to "B" for "bit". Output is capitalized to indicate it could be an array. Rename internal variable "cl" to "cls" for clarity. Rewrite cumbersome "if ((n < 1)(:))" syntax using post logical test forced column conversion to "if (n(:) < 1)". Add comment to BIST test for empty input to clarify what it is testing. Re-order input validation BIST tests for clarity. diff -r af6623656f53 -r 74089676bd9d scripts/general/bitset.m --- a/scripts/general/bitset.m Fri Apr 01 14:37:28 2022 -0700 +++ b/scripts/general/bitset.m Fri Apr 01 14:39:54 2022 -0700 @@ -24,8 +24,8 @@ ######################################################################## ## -*- texinfo -*- -## @deftypefn {} {@var{C} =} bitset (@var{A}, @var{n}) -## @deftypefnx {} {@var{C} =} bitset (@var{A}, @var{n}, @var{val}) +## @deftypefn {} {@var{B} =} bitset (@var{A}, @var{n}) +## @deftypefnx {} {@var{B} =} bitset (@var{A}, @var{n}, @var{val}) ## Set or reset bit(s) at position @var{n} of the unsigned integers in @var{A}. ## ## The least significant bit is @var{n} = 1. @w{@var{val} = 0} resets bits and @@ -62,7 +62,7 @@ ## @seealso{bitand, bitor, bitxor, bitget, bitcmp, bitshift, intmax, flintmax} ## @end deftypefn -function C = bitset (A, n, val = true) +function B = bitset (A, n, val = true) if (nargin < 2) print_usage (); @@ -79,28 +79,28 @@ ## Special case of empty input if (isempty (A)) - C = []; + B = []; return; endif sz = size (A); - cl = class (A); + cls = class (A); if (isfloat (A) && isreal (A)) - Bmax = flintmax (cl); + Bmax = flintmax (cls); Amax = ceil (log2 (Bmax)); elseif (isinteger (A)) - Bmax = intmax (cl); + Bmax = intmax (cls); Amax = ceil (log2 (Bmax)); else - error ("bitset: invalid class %s", cl); + error ("bitset: invalid class %s", cls); endif - if (any ((n < 1)(:)) || any ((n > Amax)(:))) + if (any (n(:) < 1) || any (n(:) > Amax)) error ("bitset: N must be in the range [1,%d]", Amax); endif - mask = bitshift (cast (1, cl), uint8 (n) - uint8 (1)); + mask = bitshift (cast (1, cls), uint8 (n) - uint8 (1)); on = logical (val); off = ! on; @@ -113,9 +113,9 @@ offmask = mask(off); endif - C = zeros (sz, cl); - C(on) = bitor (A(on), onmask); - C(off) = bitand (A(off), bitcmp (offmask)); + B = zeros (sz, cls); + B(on) = bitor (A(on), onmask); + B(off) = bitand (A(off), bitcmp (offmask)); endfunction @@ -131,6 +131,7 @@ %! endfor %! endfor +## Special case of empty input %!assert (bitset ([], 1), []) %!assert <*36458> (bitset (uint8 ([1, 2;3 4]), 1, [0 1; 0 1]), @@ -145,16 +146,16 @@ %!error bitset () %!error bitset (1) %!error bitset (-1, 2) +%!error bitset ([1 2], [1 2 3]) %!error bitset (1, [1 2], [1 2 3]) -%!error bitset ([1 2], [1 2 3]) %!error bitset ("1", 2) %!error bitset (0, 0) %!error bitset (0, 55) %!error bitset (single (0), 0) %!error bitset (single (0), 26) -%!error bitset (uint8 (0), 0) +%!error bitset (int8 (0), 0) +%!error bitset (int8 (0), 9) %!error bitset (uint8 (0), 9) -%!error bitset (int8 (0), 9) %!error bitset (int16 (0), 17) %!error bitset (uint16 (0), 17) %!error bitset (int32 (0), 33)