bitset - Set bit at specified position

Syntax

C = bitset(A, bit)
C = bitset(A, bit, v)

Description

C = bitset(A, bit) sets bit position bit in A to 1 (on). A must be an unsigned integer or an array of unsigned integers, and bit 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).

C = bitset(A, bit, v) sets the bit at position bit to the value v, which must be either 0 or 1.

Examples

Example 1

Setting the fifth bit in the five-bit binary representation of the integer 9 (01001) yields 11001, or 25:

C = bitset(uint8(9), 5)
C =
    25

Example 2

Repeatedly subtract powers of 2 from the largest uint32 value:

a = intmax('uint32')
for k = 1:32
   a = bitset(a, 32-k+1, 0)
   end

See Also

bitand, bitcmp, bitget, bitmax, bitor, bitshift, bitxor

  


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