Extracts from integers bits of given indices
y = bitget(x, pos)
Scalar, vector, matrix or hypermatrix of positive decimal or encoded integers.
Scalar, vector, matrix or hypermatrix of decimal or encoded integers in
[1, bitmax] where bitmax is the
maximal index of bits for the type of x: Indices of bits
to be extracted. The bit #1 is the lightest one (20).
| typeof(x) | bitmax | .. | typeof(x) | bitmax |
|---|---|---|---|---|
| int8 | 7 | uint8 | 8 | |
| int16 | 15 | uint16 | 16 | |
| int32 | 31 | uint32 | 32 | |
| int64 | 63 | uint16 | 64 | |
| decimal | 1024 |
Scalar, vector, matrix or hypermatrix of 0 and 1 of the type of
x. The sizes and contents of y are
as follows:
If x is a scalar:
y has the sizes of posy(i,j,..) is the value of bit
#pos(i,j,..) of x.If pos is a scalar:
y has the sizes of xy(i,j,..) is the value of the bit
#pos of x(i,j,..).If x and pos are arrays with
identical sizes, the processing is element-wise:
y has the sizes of x
and posy(i,j,..) is the value of the bit
#pos(i,j,..) of x(i,j,..).Otherwise:
y is a matrix with
length(x) rows and
length(pos) columns.y(i,j) is the value of the bit
#pos(j) of x(i).bitget() scans chosen bits of the binary representation of some
positive integers x. It returns 0 for bits down, and 1 for bits up.
The result has the sizes of x or of pos or
of both inputs.
However, when both x and pos are non-scalar and
have mismatching sizes, the result y is a matrix ignoring the sizes
of x. Then, after reshaping y with
y = matrix(y, [size(x) -1]), the value of the bit #b of
x(i,..,k) is in y(i,..,k,b).
// 19 is (10011)_2 // The 2nd bit is 1 (starting from the end). x=uint8(19); pos=2; y = bitget(x,pos) expected = 1; // 13 is (1101)_2 dec2bin(13) bitget(uint8(13),4:-1:1) | ![]() | ![]() |
With arrays and encoded integers::
With big decimal integers > 252:
x = sum(2 .^([7 16 18 19 25 52 70]-1)) bitget(x, [7 16 18 19 35 52 70 80]) | ![]() | ![]() |
--> x = sum(2 .^([7 16 18 19 25 52 70]-1)) x = 5.903D+20 --> bitget(x, [7 16 18 19 35 52 70 80]) ans = Nan Nan 1. 1. 0. 1. 1. 0.
x and pos are arrays with mismatching sizes:
x = [ 39 6 62 8 14 29 4 64 12 44 39 50 52 12 39 5 4 29 ]; x = sum(2.^(x-1),2); bitget(x, [5 8 12 39]) | ![]() | ![]() |
--> bitget(x, [5 8 12 39]) ans = Nan Nan 0. 1. 0. 1. 0. 0. Nan Nan 1. 0. 0. 0. 0. 1. 0. 0. 1. 1. 1. 0. 0. 0.
| Version | Description |
| 6.1 |
|