int2bit
Description
Examples
Specify a row vector of integers.
X = [12 5]
X = 1×2
12 5
Specify for four column-wise bit elements for the converted output. Then, convert the integers to bits.
n = 4; Y = int2bit(X,n)
Y = 4×2
1 0
1 1
0 0
0 1
Specify a matrix of integers.
X = int8([10 6 14; 11 5 9])
X = 2×3 int8 matrix
10 6 14
11 5 9
Specify that the first bit in each set of four column-wise bit elements for the output is LSB. Then, convert the integers to bits.
n = 4; msbfirst = false; Y = int2bit(X,n,msbfirst)
Y = 8×3 int8 matrix
0 0 0
1 1 1
0 1 1
1 0 1
1 1 1
1 0 0
0 1 0
1 0 1
Specify an array of integers.
X = randi([0,9],4,3,2,'uint16')
X = 4×3×2 uint16 array
X(:,:,1) =
8 6 9
9 0 9
1 2 1
9 5 9
X(:,:,2) =
9 4 6
4 9 0
8 7 8
1 9 9
Specify for three column-wise bit elements for the converted output. Then, convert the integers to bits.
n = 3; Y = int2bit(X,n)
Y = 12×3×2 uint8 array
Y(:,:,1) =
0 1 0
0 1 0
0 0 1
0 0 0
0 0 0
1 0 1
0 0 0
0 1 0
1 0 1
0 1 0
0 0 0
1 1 1
Y(:,:,2) =
0 1 1
0 0 1
1 0 0
1 0 0
0 0 0
0 1 0
0 1 0
0 1 0
0 1 0
0 0 0
0 0 0
1 1 1
Specify a row vector of integers.
X = [153, -103, 103, -128]
X = 1×4
153 -103 103 -128
Specify for eight column-wise bit elements for the converted output. Then, convert the integers to bits.
n = 8; Y1 = int2bit(X,n)
Y1 = 8×4
1 1 0 1
0 0 1 0
0 0 1 0
1 1 0 0
1 1 0 0
0 0 1 0
0 0 1 0
1 1 1 0
Convert the bits back to integers and check the values. The resulting integers do not match the original integers, because the signed integers require more than eight bits for correct representation as twos-complement values.
X1 = bit2int(Y1,n,IsSigned=true)
X1 = 1×4
-103 -103 103 -128
isequal(X,X1)
ans = logical
0
Specify for nine column-wise bit elements for the converted output. Then, convert the integers to bits.
n = 9; Y2 = int2bit(X,n)
Y2 = 9×4
0 1 0 1
1 1 0 1
0 0 1 0
0 0 1 0
1 1 0 0
1 1 0 0
0 0 1 0
0 0 1 0
1 1 1 0
Convert the bits back to integers and check the values. The resulting integers match the original integers, confirming that nine bits correctly represents the twos-complement values.
X2 = bit2int(Y2,n,IsSigned=true)
X2 = 1×4
153 -103 103 -128
isequal(X,X2)
ans = logical
1
Input Arguments
Integers, specified as a scalar, vector, matrix, or 3-D array of integer values.
Example: [-10 2]
specifies an input row vector of size
1-by-2.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
The number of bits for conversion to bits, specified as a positive integer. The
number of bits, n
, includes the signed bit.
Data Types: double
Output Arguments
Bit representation of input integers, returned as a column vector, matrix, or 3-D
array. Y
has the same dimensions as X
except
that the number of rows in Y
is n
times the
number of rows in X
. The output Y
consists of
n
least significant bits in the specified orientation. If
n
is less than the number of required bits to represent the
values in X
, then the output Y
consists of
n
least significant bits.
The data type of Y
depends on the data type of
X
.
If
X
is a floating-point data type, thenY
is a floating-point data type.If
X
is a built-in unsigned integer data type, thenY
is of data typeuint8
.If
X
is a built-in signed integer data type, thenY
is of data typeint8
.If
X
is of data typedouble
, thenY
is of data typedouble
withn
no larger than 53.If
X
is of data typesingle
, thenY
is of data typesingle
withn
no larger than 24.
Extended Capabilities
Usage notes and limitations:
Input values must be less than 264 for code generation when the input data type is double.
This function supports GPU array inputs. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced in R2021bThe int2bit
function adds support for gpuArray
(Parallel Computing Toolbox) object processing to run code on a graphics processing unit
(GPU).
int2bit
now supports conversion of signed integer inputs to
binary values output as 2s-complement values.
In prior releases, negative input integers caused an error.
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)