bitget
Bit at specified position
Syntax
C = bitget(A, bit)
Description
C = bitget(A, bit) returns
the value of the bit at
position bit in A.
Operand A must be an
unsigned integer, a double, or an array containing unsigned
integers, doubles or both.
The bit input
must be a number between 1 and the number of bits in the unsigned
integer class
of A (e.g., 32 for
the uint32 class).
The output of bitget is
independent of the endian settings of the computer you are
using.
Examples
Example 1 — Binary Conversion
The dec2bin function
converts decimal numbers to binary. However, you can also use
the bitget function
to show the binary representation of a decimal number. Just test
successive bits from most to least significant:
disp(dec2bin(13)) 1101 C = bitget(uint8(13), 4:-1:1) C = 1 1 0 1
Example 2 — Binary Compare
Prove that intmax sets
all the bits to 1:
a = intmax('uint8'); if all(bitget(a, 1:8)) disp('All the bits have value 1.') end All the bits have value 1.
Example 3 — Vector and Array Operations
Get the value of the second most significant bit of the number
sequence 5 through 75, counting by tens:
bitget(5:10:75, [2 3 4 5 5 5 6 7]) ans = 0 1 1 0 0 1 0 1
Do the same, but using 2-by-4 matrices:
bitget([5 15 25 35; 45 55 65 75], ... [2 3 4 5; 5 5 6 7]) ans = 0 1 1 0 0 1 0 1
See Also
bitand, bitcmp, bitmax, bitor, bitset,bitshift,bitxor
> c =
bitget(2,5:-1:1)
c =
0
0 0 1
0
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
dec2bin
Convert decimal to binary number in string
Syntax
str
= dec2bin(d)
str = dec2bin(d,n)
Description
returns the
str
= dec2bin(d) binary
representation of d as a
string. d must be a nonnegative
integer smaller than 2^52.
str
= dec2bin(d,n) produces
a binary representation with at
least n bits.
The output of dec2bin is independent of
the endian settings of the computer you are using.
Examples
Decimal 23 converts to binary 010111:
dec2bin(23) ans = 10111