Bits
GetBits
extract specified bits from a number
Calling Sequence
Parameters
Description
Examples
GetBits(number, locns)
GetBits(number, locns, options)
number
-
an integer
locns
a single or sequence of locations or ranges
options
optional arguments
The GetBits command extracts the specified bits from a number. The locns argument can be a sequence of bit locations or bit location ranges, including negative ranges and negative offsets. Note that all bit locations are given with 0 as the location of the least significant bit. For example, -1..0 can be used to extract the bits in a number in reverse order.
The optional arguments for GetBits are as follows:
output=form: where form can be sequence (the default), or number. When form is specified as sequence, the output will be in the form of a sequence of 0's and 1's corresponding to the specified bit locations. When form is specified as number, the bit sequence will be used to construct a number, where the first bit in locns will be the least significant bit in the output number.
bits=count: specifies the number of bits in the input number. Any bit references that exceed this value will have a value of zero. Any negative bit references that go past the least significant bit will return an error. The default bits value can be set via Settings.
with⁡Bits:
num≔Join⁡1,1,0,0,0,1,0,1,0,1,1,0,1,1,1
num≔30371
Get least significant bit.
GetBits⁡num,0
1
Get most significant bit. (This will be 1 if bits is not specified.)
GetBits⁡num,−1
Get most significant bit assuming input is 16 bits
GetBits⁡num,−1,bits=16
0
Extract all bits - this is the same as Split
GetBits⁡num,0..−1
1,1,0,0,0,1,0,1,0,1,1,0,1,1,1
Split⁡num
Reverse order of the bits as a 16 bit number
GetBits⁡num,−1..0,bits=16
0,1,1,1,0,1,1,0,1,0,1,0,0,0,1,1
Reverse order as a 16 bit number with number output is equivalent to extracting in reverse above followed by join
GetBits⁡num,−1..0,output=number,bits=16
50542
Join⁡GetBits⁡num,−1..0,bits=16
Exchange first 8 bits and last 8 bits in a 16 bit number
num2≔GetBits⁡num,8..15,0..7,output=number
num2≔41846
Check:
convert⁡num,hex
76A3
convert⁡num2,hex
A376
See Also
convert/hex
Join
Settings
Split
Download Help Document