bit2int
Convert bits to integers
Description
Examples
Convert Vector of Bits to Integers
Specify a column vector of bits.
X = [1 0 1 0 1 0 1 0]';
Specify for four column-wise bit elements of the input vector to be converted to integer values. Then, convert the bits to integers.
n = 4; Y = bit2int(X,n)
Y = 2×1
10
10
Convert Matrix of Bits to Integers
Specify a matrix of bits.
X = int8([1 1 0; 0 1 1]')
X = 3x2 int8 matrix
1 0
1 1
0 1
Specify that the first bit in each set of three column-wise bit elements is the LSB. Then, convert the bits to integers.
n = 3; msbfirst = false; Y = bit2int(X,n,msbfirst)
Y = 1x2 int8 row vector
3 6
Convert Array of Bits to Integers
Specify an array of bits.
X = randi([0,1],8,2,2,'uint8')
X = 8x2x2 uint8 array
X(:,:,1) =
1 1
1 1
0 0
1 1
1 1
0 0
0 1
1 0
X(:,:,2) =
0 1
1 1
1 1
1 0
1 1
0 0
1 1
1 0
Specify that the first bit in each set of four column-wise bit elements is the MSB. Then, convert the bits to integers.
n = 4; msbfirst = true; Y = bit2int(X,n,msbfirst)
Y = 2x2x2 uint8 array
Y(:,:,1) =
13 13
9 10
Y(:,:,2) =
7 14
11 10
Input Arguments
X
— Bits
column vector | matrix | 3-D array
Bits, specified as a column vector, matrix, or 3-D array of numeric or logical
0
s and 1
s.
Example: [1 0 1 0 1 0 1 0]'
specifies an input column vector of
size 8-by-1.
Note
The number of rows in X
must be a multiple of input
n
.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
n
— Number of bits to be converted
positive integer
Number of bits to be converted to integers, specified as a positive integer.
Data Types: double
msbfirst
— Specification of MSB first
true
or 1
| false
or 0
Output Arguments
Y
— Integer representation of input bits
scalar | column vector | matrix | 3-D array
Integer representation of input bits, returned as a scalar, column vector, matrix,
or 3-D array. The function returns the integer-equivalent value for each set of
n
column-wise bits in X
. Output
Y
has same dimensions as input X
except that
the number of rows in Y
is n
times less than
the number of rows in X
.
The data type of Y
depends on the data type of
X
.
If
X
is of data typedouble
orlogical
, thenY
is of data typedouble
.If
X
is of data typesingle
, thenY
is of data typesingle
.If
X
is an integer data type and the value ofY
can be contained in the same integer data type, thenY
is of the same data type and signedness asX
. If the value ofY
cannot be contained in the same integer data type asX
, then the function sets the data type ofY
to an integer data type that is big enough to contain its value.
Extended Capabilities
Version History
Introduced in R2021b
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
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)