Products & Services Industries Academia Support User Community Company

Learn more about MATLAB   

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

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:65, [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

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS