octave: Bit Manipulations
4.5 Bit Manipulations
=====================
Octave provides a number of functions for the manipulation of numeric
values on a bit by bit basis. The basic functions to set and obtain the
values of individual bits are ‘bitset’ and ‘bitget’.
-- : C = bitset (A, N)
-- : C = bitset (A, N, VAL)
Set or reset bit(s) N of the unsigned integers in A.
VAL = 0 resets and VAL = 1 sets the bits. The least significant
bit is N = 1. All variables must be the same size or scalars.
dec2bin (bitset (10, 1))
⇒ 1011
DONTPRINTYET See also: bitand XREFbitand, bitor XREFbitor, *noteDONTPRINTYET DONTPRINTYET See also: bitand XREFbitand, bitor XREFbitor,
bitxor XREFbitxor, bitget XREFbitget, *notebitcmp:
DONTPRINTYET DONTPRINTYET See also: bitand XREFbitand, bitor XREFbitor,
bitxor XREFbitxor, bitget XREFbitget, bitcmp
XREFbitcmp, bitshift XREFbitshift, intmax XREFintmax,
flintmax XREFflintmax.
-- : C = bitget (A, N)
Return the status of bit(s) N of the unsigned integers in A.
The least significant bit is N = 1.
bitget (100, 8:-1:1)
⇒ 0 1 1 0 0 1 0 0
DONTPRINTYET See also: bitand XREFbitand, bitor XREFbitor, *noteDONTPRINTYET DONTPRINTYET See also: bitand XREFbitand, bitor XREFbitor,
bitxor XREFbitxor, bitset XREFbitset, *notebitcmp:
DONTPRINTYET DONTPRINTYET See also: bitand XREFbitand, bitor XREFbitor,
bitxor XREFbitxor, bitset XREFbitset, bitcmp
XREFbitcmp, bitshift XREFbitshift, intmax XREFintmax,
flintmax XREFflintmax.
The arguments to all of Octave’s bitwise operations can be scalar or
arrays, except for ‘bitcmp’, whose K argument must a scalar. In the
case where more than one argument is an array, then all arguments must
have the same shape, and the bitwise operator is applied to each of the
elements of the argument individually. If at least one argument is a
scalar and one an array, then the scalar argument is duplicated.
Therefore
bitget (100, 8:-1:1)
is the same as
bitget (100 * ones (1, 8), 8:-1:1)
It should be noted that all values passed to the bit manipulation
functions of Octave are treated as integers. Therefore, even though the
example for ‘bitset’ above passes the floating point value ‘10’, it is
treated as the bits ‘[1, 0, 1, 0]’ rather than the bits of the native
floating point format representation of ‘10’.
As the maximum value that can be represented by a number is important
for bit manipulation, particularly when forming masks, Octave supplies
two utility functions: ‘flintmax’ for floating point integers, and
‘intmax’ for integer objects (‘uint8’, ‘int64’, etc.).
Octave also includes the basic bitwise ’and’, ’or’, and ’exclusive
or’ operators.
-- : bitand (X, Y)
Return the bitwise AND of non-negative integers.
X, Y must be in the range [0,intmax]
DONTPRINTYET See also: bitor XREFbitor, bitxor XREFbitxor, *noteDONTPRINTYET DONTPRINTYET See also: bitor XREFbitor, bitxor XREFbitxor,
bitset XREFbitset, bitget XREFbitget, *notebitcmp:
DONTPRINTYET DONTPRINTYET See also: bitor XREFbitor, bitxor XREFbitxor,
bitset XREFbitset, bitget XREFbitget, bitcmp
XREFbitcmp, bitshift XREFbitshift, intmax XREFintmax,
flintmax XREFflintmax.
-- : bitor (X, Y)
Return the bitwise OR of non-negative integers X and Y.
DONTPRINTYET See also: bitor XREFbitor, bitxor XREFbitxor, *noteDONTPRINTYET DONTPRINTYET See also: bitor XREFbitor, bitxor XREFbitxor,
bitset XREFbitset, bitget XREFbitget, *notebitcmp:
DONTPRINTYET DONTPRINTYET See also: bitor XREFbitor, bitxor XREFbitxor,
bitset XREFbitset, bitget XREFbitget, bitcmp
XREFbitcmp, bitshift XREFbitshift, intmax XREFintmax,
flintmax XREFflintmax.
-- : bitxor (X, Y)
Return the bitwise XOR of non-negative integers X and Y.
DONTPRINTYET See also: bitand XREFbitand, bitor XREFbitor, *noteDONTPRINTYET DONTPRINTYET See also: bitand XREFbitand, bitor XREFbitor,
bitset XREFbitset, bitget XREFbitget, *notebitcmp:
DONTPRINTYET DONTPRINTYET See also: bitand XREFbitand, bitor XREFbitor,
bitset XREFbitset, bitget XREFbitget, bitcmp
XREFbitcmp, bitshift XREFbitshift, intmax XREFintmax,
flintmax XREFflintmax.
The bitwise ’not’ operator is a unary operator that performs a
logical negation of each of the bits of the value. For this to make
sense, the mask against which the value is negated must be defined.
Octave’s bitwise ’not’ operator is ‘bitcmp’.
-- : bitcmp (A, K)
Return the K-bit complement of integers in A.
If K is omitted ‘k = log2 (flintmax) + 1’ is assumed.
bitcmp (7,4)
⇒ 8
dec2bin (11)
⇒ 1011
dec2bin (bitcmp (11, 6))
⇒ 110100
DONTPRINTYET See also: bitand XREFbitand, bitor XREFbitor, *noteDONTPRINTYET DONTPRINTYET See also: bitand XREFbitand, bitor XREFbitor,
bitxor XREFbitxor, bitset XREFbitset, *notebitget:
DONTPRINTYET DONTPRINTYET See also: bitand XREFbitand, bitor XREFbitor,
bitxor XREFbitxor, bitset XREFbitset, bitget
XREFbitget, bitcmp XREFbitcmp, bitshift XREFbitshift,
flintmax XREFflintmax.
Octave also includes the ability to left-shift and right-shift values
bitwise.
-- : bitshift (A, K)
-- : bitshift (A, K, N)
Return a K bit shift of N-digit unsigned integers in A.
A positive K leads to a left shift; A negative value to a right
shift.
If N is omitted it defaults to 64. N must be in the range [1,64].
bitshift (eye (3), 1)
⇒
2 0 0
0 2 0
0 0 2
bitshift (10, [-2, -1, 0, 1, 2])
⇒ 2 5 10 20 40
DONTPRINTYET See also: bitand XREFbitand, bitor XREFbitor, *noteDONTPRINTYET DONTPRINTYET See also: bitand XREFbitand, bitor XREFbitor,
bitxor XREFbitxor, bitset XREFbitset, *notebitget:
DONTPRINTYET DONTPRINTYET See also: bitand XREFbitand, bitor XREFbitor,
bitxor XREFbitxor, bitset XREFbitset, bitget
XREFbitget, bitcmp XREFbitcmp, intmax XREFintmax,
flintmax XREFflintmax.
Bits that are shifted out of either end of the value are lost.
Octave also uses arithmetic shifts, where the sign bit of the value is
kept during a right shift. For example:
bitshift (-10, -1)
⇒ -5
bitshift (int8 (-1), -1)
⇒ -1
Note that ‘bitshift (int8 (-1), -1)’ is ‘-1’ since the bit
representation of ‘-1’ in the ‘int8’ data type is ‘[1, 1, 1, 1, 1, 1, 1,
1]’.