int2bit
Description
Examples
Convert Vector of Integers to Bits
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
Convert Matrix of Integers to Bits
Specify a matrix of integers.
X = int8([10 6 14; 11 5 9])
X = 2x3 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 = 8x3 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
Convert Array of Integers to Bits
Specify an array of integers.
X = randi([0,9],4,3,2,'uint16')
X = 4x3x2 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 = 12x3x2 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
Convert Vector of Signed Integers to Bits
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
X
— Integers
scalar | vector | matrix | 3-D array
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
n
— Number of bits for conversion
positive integer
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
msbfirst
— Specification of MSB first
true
or 1
(default) | false
or 0
Output Arguments
Y
— Bit representation of input integers
column vector | matrix | 3-D array
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
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
Input values must be less than 264 for code generation when the input data type is double.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function supports GPU array inputs. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Version History
Introduced in R2021bR2024a: Add GPU array support
The int2bit
function adds support for gpuArray
(Parallel Computing Toolbox) object processing to run code on a graphics processing unit
(GPU).
R2023a: Adds support for signed input
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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)