bitset - Set bit at specific location - MATLAB (original) (raw)

Set bit at specific location

Syntax

Description

[intout](#bth5reg-intout) = bitset([A](#bth5reg-A),[bit](#bth5reg-bit)) returns the value of A with position bit set to 1 (on).

example

[intout](#bth5reg-intout) = bitset([A](#bth5reg-A),[bit](#bth5reg-bit),[assumedtype](#bth5reg-assumedtype)) assumes A is of type assumedtype.

example

[intout](#bth5reg-intout) = bitset([A](#bth5reg-A),[bit](#bth5reg-bit),[V](#bth5reg-V)) returns A with position bit set to the value of V.

example

[intout](#bth5reg-intout) = bitset([A](#bth5reg-A),[bit](#bth5reg-bit),[V](#bth5reg-V),[assumedtype](#bth5reg-assumedtype)) assumes A is of type assumedtype.

example

Examples

collapse all

Add powers of 2 onto a number.

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

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

c = 3×6 char array '001100' '010100' '100100'

MATLAB® throws an error if you specify an integer outside the range of assumedtype. For instance, bitset(300,5,'int8') returns an error since the maximum value of an int8 integer is 127.

Avoid this error by limiting your input to the range of the specified data type.

intout = bitset(75,5,'int8')

Repeatedly subtract powers of 2 from a number.

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

b = 1×8 uint8 row vector

127 63 31 15 7 3 1 0

Set multiple bits to different values

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

Input Arguments

collapse all

Input values, specified as an array. A, bit, and V can each be scalars or arrays of the same size.

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

Bit position, specified as an integer or integer array. A, bit, and V can each be scalars or arrays of the same size. The values of 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

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

Data Types: char | string

Bit value, specified as a scalar or a numeric array. A, bit, and V can each be scalars or arrays of the same size.

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

Output Arguments

collapse all

Adjusted integers, returned as an array. intout is the same data type as A.

Extended Capabilities

expand all

The bitset function supports GPU array input with these usage notes and limitations:

For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Version History

Introduced before R2006a