bitset

Set bit at specific location

Syntax

  • intout = bitset(A,bit) example
  • intout = bitset(A,bit,assumedtype) example
  • intout = bitset(A,bit,V) example
  • intout = bitset(A,bit,V,assumedtype) example

Description

example

intout = bitset(A,bit) returns the value of A with position bit set to 1 (on).

example

intout = bitset(A,bit,assumedtype) assumes A is of type assumedtype.

example

intout = bitset(A,bit,V) returns A with position bit set to the value of V.

  • If V is zero, then the bit position bit is set to 0 (off).

  • If V is nonzero, then the bit position bit is set to 1 (on).

example

intout = bitset(A,bit,V,assumedtype) assumes A is of type assumedtype.

Examples

expand all

Set Bits to On

Add powers of 2 onto a number.

A = 4;
intout = bitset(A,4:6)
intout =

    12    20    36

You can see that bitset sequentially turns on bits 4 through 6.

c = dec2bin(intout)
c =

001100
010100
100100

Out of Range of Integer Type

MATLAB® throws an error if you specify an integer outside the range of assumedtype.

intout = bitset(300,5,'int8')
Error using bitset
Double inputs must have integer values in the range of ASSUMEDTYPE.

You can avoid this error by limiting your input to the range of the specified datatype.

Set Bits to Off

Repeatedly subtract powers of 2 from a number.

a = intmax('uint8')
for k = 0:7
   a = bitset(a, 8-k, 0);
   b(1,k+1) = a;
end
b
a =

  255


b =

   127    63    31    15     7     3     1     0

Set Multiple Bits

Set multiple bits to different values

bits = 2:6;
val = [1 0 0 1 1];
intout = bitset(0,bits,val,'int8')
intout = 

      2     0     0    16    32

Input Arguments

expand all

A — Input valuesigned integer array | unsigned integer array | double array

Input value, specified as a signed integer array, unsigned integer array, or double array.

  • If A is a double array, and assumedtype is not specified, then MATLAB treats A as an unsigned 64-bit integer.

  • If assumedtype is specified, then all elements in A must have integer values within the range of assumedtype.

Data Types: double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

bit — Bit positioninteger | integer array

Bit position, specified as an integer or integer array. bit must be between 1 (the least significant bit) and the number of bits in the integer class of A.

Data Types: double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

assumedtype — Assumed data type of A 'uint64' | 'uint32' | 'uint16' | 'uint8' | 'int64' | 'int32' | 'int16' | 'int8'

Assumed data type of A, specified as 'uint64', 'uint32', 'uint16', 'uint8', 'int64', 'int32', 'int16', or 'int8'.

  • If A is a double array, then assumedtype can specify any valid integer type, but defaults to 'uint64'.

  • If A is an integer type array, then assumedtype must specify that same integer type.

Data Types: char

V — bit valuescalar | numeric array

Bit value, specified as a scalar or a numeric array. If V and bit are arrays, they must be the same size.

Data Types: double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

Output Arguments

expand all

intout — Adjusted integersigned integer array | unsigned integer array | double array

Adjusted integer, returned as a signed integer array, unsigned integer array, or double array. intout is the same size and type as A.

Was this topic helpful?