bitget

Reads bits of an integer at given positions.

Category: Language Fundamentals > Operators and Elementary Operations

Syntax

b = bitget(A, n)

Returns bit n of A, counting from 1 at the least significant bit.

Description

Bit 1 is the units digit in binary, bit 2 the twos, bit 3 the fours, and so on. Passing a vector of positions reads several bits at once, so bitget(uint8(5), 8:-1:1) spells out the binary digits of 5, most significant first.

The counterpart bitset writes a bit.

Input arguments

A
Array of nonnegative integers.
n
Bit position or positions, from 1 up to the width of the class (53 for doubles).

Output arguments

b
Array of 0 and 1 of the same class as A.

Example

bitget(uint8(5), 1:3)
bitget(uint8(5), 8:-1:1)
bitget(10, 1:4)
Output
ans = 1×3 uint8 row vector

   1   0   1
ans = 1×8 uint8 row vector

   0   0   0   0   0   1   0   1
ans = 1×4

     0     1     0     1

See also

bitset bitand bitshift dec2bin