bitset
Set bit at specific location
Syntax
Description
assumes intout
= bitset(A
,bit
,assumedtype
)A
is
of type assumedtype
.
Examples
Set Bits to On
Add powers of 2 onto a number.
A = 4; intout = bitset(A,4:6)
intout = 1×3
12 20 36
You can see that bitset
sequentially turns on bits 4 through 6.
c = dec2bin(intout)
c = 3x6 char array
'001100'
'010100'
'100100'
Out of Range of Integer Type
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')
intout = 91
Set Bits to Off
Repeatedly subtract powers of 2 from a number.
a = intmax('uint8')
a = uint8
255
for k = 0:7 a = bitset(a, 8-k, 0); b(1,k+1) = a; end b
b = 1x8 uint8 row vector
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 = 1×5
2 0 0 16 32
Input Arguments
A
— Input values
array
Input values, specified as an array. A
, bit
,
and V
can each be scalars or arrays of the same
size.
If
A
is a double array, andassumedtype
is not specified, then MATLAB® treatsA
as an unsigned 64-bit integer.If
assumedtype
is specified, then all elements inA
must have integer values within the range ofassumedtype
.
Data Types: double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
bit
— Bit position
integer | integer array
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
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, thenassumedtype
can specify any valid integer type, but defaults to'uint64'
.If
A
is an integer type array, thenassumedtype
must specify that same integer type.
Data Types: char
| string
V
— bit value
scalar | numeric array
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
intout
— Adjusted integers
array
Adjusted integers, returned as an array. intout
is
the same data type as A
.
If
A
,bit
, andV
are all scalars, thenintout
is also a scalar.If any of
A
,bit
, orV
is an array, thenintout
is the same size as that array.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.
HDL Code Generation
Generate VHDL, Verilog and SystemVerilog code for FPGA and ASIC designs using HDL Coder™.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
The bitset
function
supports GPU array input with these usage notes and limitations:
At least one of input values
A
and bit positionbit
must be an integer data type.The
assumedtype
argument is not supported.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced before R2006a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)