Library bitfield implements the constructor and methods of class bitfield for bit fields (binary numbers). Basic arithmetic operators and functions are overloaded to support expressions with the same syntax as for numbers and matrices. Contrary to integer numbers, bitfield objects have a length (between 1 and 32) and are displayed in binary.
The following statement makes available functions defined in bitfield:
use bitfield
First bit position in a bitfield.
a(...beginning...)
In the index expression of a bitfield, beginning is the position of the least-significant bit, i.e. 0.
bitfield::bitfield, bitfield::end
Bitfield object constructor.
a = bitfield a = bitfield(n) a = bitfield(n, wordlength)
bitfield(n,wordlength) creates a bitfield object initialized with the wordlength least significant bits of the nonnegative integer number n. The default value of wordlength is 32 if n is a double, an int32 or a uint32 number; 16 is n is an int16 or uint16 number; or 8 if n is an int8 or uint8 number. Without argument, bitfield gives a bit field of 32 bits 0. Like any integer number in LME, n may be written in base 2, 8, 10, or 16: 0b1100, 014, 12, and 0xc all represent the same number.
The following operators and functions may be used with bitfield arguments, with results analog to the corresponding functions of LME. Logical functions operate bitwise.
|
|
Indexes into bit fields are non-negative integers: 0 represents the least-significant bit, and wordlength-1 the most-significant bit. Unlike arrays, bits are not selected with logical arrays, but with other bit fields where ones represent the bits to be selected; for example a(0b1011) selects bits 0, 1 and 3. This is consistent with the way bitfield::find is defined.
a = bitfield(123, 16) a = 0b0000000001111011 b = ~a b = 0b1111111110000100 b = a * 5 b = 0b0000001001100111
bitfield::disp, bitfield::double
Display a bitfield object.
disp(a)
disp(a) displays bitfield a. It is also executed implicitly when LME displays the bitfield result of an expression which does not end with a semicolon.
Convert a bitfield object to a double number.
n = double(a)
double(a) converts bitfield a to double number.
a = bitfield(123, 16); double(a) 123
Last bit position in a bitfield.
a(...end...)
In the index expression of a bitfield, end is the position of the most-significant bit, i.e. 1 less than the word length.
bitfield::bitfield, bitfield::beginning
Find the ones in a bitfield.
ix = find(a)
find(a) finds the bits equal to 1 in bitfield a. The result is a vector of bit positions in ascending order; the least-significant bit is number 0.
a = bitfield(123, 16) a = 0b0000000001111011 ix = find(a) ix = 0 1 3 4 5 6
Convert a bitfield object to a signed integer number, with sign extension.
n = int8(a) n = int16(a) n = int32(a)
int8(a), int16(a), and int32(a) convert bitfield a to an int8, int16, or int32 number respectively. If a has less bits than the target integer and the most significant bit of a is 1, sign extension is performed; i.e. the most significant bits of the result are set to 1, so that it is negative. If a has more bits than the target integer, most significant bits are ignored.
a = bitfield(9, 4); a = 0x1001 i = int8(a) i = 210 b = bitfield(i) b = 0b11111001
uint8, uint16, uint32, bitfield::int8, bitfield::int16, bitfield::int32, bitfield::double, bitfield::bitfield
Word length of a bitfield.
wordlength = length(a)
length(a) gives the number of bits of bitfield a.
a = bitfield(123, 16); length(a) 16
Get the sign of a bitfield.
s = sign(a)
sign(a) gets the sign of bitfield a. The result is -1 if the most-significant bit of a is 1, 0 if all bits of a are 0, or 1 otherwise.
a = bitfield(5, 3) a = 0b101 sign(a) -1
Convert a bitfield object to an unsigned integer number.
n = uint8(a) n = uint16(a) n = uint32(a)
uint8(a), uint16(a), and uint32(a) convert bitfield a to a uint8, uint16, or uint32 number respectively. If a has more bits than the target integer, most significant bits are ignored.
a = bitfield(1234, 16); uint8(a) 210
uint8, uint16, uint32, bitfield::int8, bitfield::int16, bitfield::int32, bitfield::double, bitfield::bitfield