bitset
Set bit at specific location
Description
Examples
Set Bit at Certain Position
Begin with an unsigned fixed-point fi
number with a value of 5, word length 4, and fraction length 0.
a = fi(5,0,4,0); disp(bin(a))
0101
Set the bit at position 4 to 1
(on).
c = bitset(a,4); disp(bin(c))
1101
Set Bit at Certain Position in Vector
Consider the following fixed-point vector with word length 4 and fraction length 0.
a = fi([0 1 8 2 4],0,4,0); disp(bin(a))
0000 0001 1000 0010 0100
In each element of vector a
, set the bits at position 2 to 1
.
c = bitset(a,2,1); disp(bin(c))
0010 0011 1010 0010 0110
Set Bit at Certain Position with Fixed Point Index
Consider the following fixed-point scalar with a value of 5.
a = fi(5,0,4,0); disp(bin(a))
0101
Set the bit at position fi(2)
to 1
.
c = bitset(a,fi(2),1); disp(bin(c))
0111
Set Bit When Index Is Vector
Create a fi
object with a value of pi
.
a = fi(pi); disp(bin(a))
0110010010001000
In this case, a
is signed with a word length of 16.
Create a vector of the bit positions in a
that you want to set to on. Then, get the binary representation of the resulting fi
vector.
bit = fi([15,3,8,2]); c = bitset(a,bit); disp(bin(c))
0110010010001000 0110010010001100 0110010010001000 0110010010001010
Input Arguments
A
— Input values
scalar | vector | matrix | multidimensional array
Input values, specified as a scalar, vector, matrix, or multidimensional array of
fixed-point fi
objects. If any of
A
, bit
, or V
are
nonscalar, the other inputs must be scalar or arrays of the same size. If
A
has a signed numerictype
, the bit representation of the stored integer is in two's
complement representation.
Data Types: fi
Complex Number Support: Yes
bit
— Bit position
integer | integer array
Bit position, specified as an integer or integer array of fi
objects or built-in data types. If any of A
,
bit
, or V
are nonscalar, the other inputs
must be scalar or arrays of the same size. The values of bit
must
be between 1
and the word length of A
,
inclusive. The LSB
, the right-most bit, is specified by bit index
1
. The MSB
, the left-most bit, is specified by
the word length of A
.
a = fi(pi,0,8); a.bin
ans = '11001001'
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| fi
V
— Bit value
scalar | vector | matrix | multidimensional array
Bit value of A
at index bit
, specified as
a scalar, vector, matrix, or multidimensional array of fi
objects or
built-in data types. If any of A
, bit
, or
V
are nonscalar, the other inputs must be scalar or arrays of the
same size. V
can have values of 0
or
1
. Any value other than 0
is automatically set
to 1
.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| fi
Complex Number Support: Yes
Output Arguments
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
HDL Code Generation
Generate VHDL, Verilog and SystemVerilog code for FPGA and ASIC designs using HDL Coder™.
Version History
Introduced before R2006aR2022a: Scalar expansion support for fi
bitset
Prior to R2022a, fi
bitset
required that the second and third input arguments be the same
size, otherwise an error would occur.
A = fi(pi); disp(bin(A)) bit = fi([15,3,8,2]); C = bitset(A,bit,1); disp(bin(C))
0110010010001000 The Second and third arguments to BITSET must be the same size.
Starting in R2022a, the input arguments A
,
bit
, and V
support scalar expansion. That is, if
any of A
, bit
, or V
are
nonscalar, the other inputs can be scalar or arrays of the same size.
A = fi(pi); disp(bin(A)) bit = fi([15,3,8,2]); C = bitset(A,bit,1); disp(bin(C))
0110010010001000 0110010010001000 0110010010001100 0110010010001000 0110010010001010
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)