Bits - Maple Programming Help

Home : Support : Online Help : Programming : Bits : Bits/GetBits

Bits

 GetBits
 extract specified bits from a number

 Calling Sequence GetBits(number, locns) GetBits(number, locns, options)

Parameters

 number - an integer locns - a single or sequence of locations or ranges options - optional arguments

Description

 • 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.

Examples

 > $\mathrm{with}\left(\mathrm{Bits}\right):$
 > $\mathrm{num}≔\mathrm{Join}\left(\left[1,1,0,0,0,1,0,1,0,1,1,0,1,1,1\right]\right)$
 ${\mathrm{num}}{:=}{30371}$ (1)

Get least significant bit.

 > $\mathrm{GetBits}\left(\mathrm{num},0\right)$
 ${1}$ (2)

Get most significant bit. (This will be 1 if bits is not specified.)

 > $\mathrm{GetBits}\left(\mathrm{num},-1\right)$
 ${1}$ (3)

Get most significant bit assuming input is 16 bits

 > $\mathrm{GetBits}\left(\mathrm{num},-1,\mathrm{bits}=16\right)$
 ${0}$ (4)

Extract all bits - this is the same as Split

 > $\left[\mathrm{GetBits}\left(\mathrm{num},0..-1\right)\right]$
 $\left[{1}{,}{1}{,}{0}{,}{0}{,}{0}{,}{1}{,}{0}{,}{1}{,}{0}{,}{1}{,}{1}{,}{0}{,}{1}{,}{1}{,}{1}\right]$ (5)
 > $\mathrm{Split}\left(\mathrm{num}\right)$
 $\left[{1}{,}{1}{,}{0}{,}{0}{,}{0}{,}{1}{,}{0}{,}{1}{,}{0}{,}{1}{,}{1}{,}{0}{,}{1}{,}{1}{,}{1}\right]$ (6)

Reverse order of the bits as a 16 bit number

 > $\left[\mathrm{GetBits}\left(\mathrm{num},-1..0,\mathrm{bits}=16\right)\right]$
 $\left[{0}{,}{1}{,}{1}{,}{1}{,}{0}{,}{1}{,}{1}{,}{0}{,}{1}{,}{0}{,}{1}{,}{0}{,}{0}{,}{0}{,}{1}{,}{1}\right]$ (7)

Reverse order as a 16 bit number with number output is equivalent to extracting in reverse above followed by join

 > $\mathrm{GetBits}\left(\mathrm{num},-1..0,\mathrm{output}=\mathrm{number},\mathrm{bits}=16\right)$
 ${50542}$ (8)
 > $\mathrm{Join}\left(\left[\mathrm{GetBits}\left(\mathrm{num},-1..0,\mathrm{bits}=16\right)\right]\right)$
 ${50542}$ (9)

Exchange first 8 bits and last 8 bits in a 16 bit number

 > $\mathrm{num2}≔\mathrm{GetBits}\left(\mathrm{num},8..15,0..7,\mathrm{output}=\mathrm{number}\right)$
 ${\mathrm{num2}}{:=}{41846}$ (10)

Check:

 > $\mathrm{convert}\left(\mathrm{num},\mathrm{hex},\mathrm{decimal}\right)$
 ${\mathrm{76A3}}$ (11)
 > $\mathrm{convert}\left(\mathrm{num2},\mathrm{hex},\mathrm{decimal}\right)$
 ${\mathrm{A376}}$ (12)