# bit2int

## Description

`Y = bit2int(___,IsSigned=`

specifies optional Name-Value pair `tf`

)`IsSigned=tf`

. The value for
`tf`

is a logical, either `true`

or
`false`

to indicate the signedness of the integer. The default is
`false`

. When you set `tf`

to `true`

,
the first bit in each block of `n`

bits is considered to be a signed bit
and the output may contain negative values. If the datatype of `X`

is any
of the unsigned integer types and you set `tf`

to `true`

,
then the datatype of `Y`

is the smallest signed integer type that can
support the number of input bits.

## 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

### Convert Array of Signed Bits to Integers

Specify an array of bits.

X = [1 1 0 1 1 0 0 1; 1 0 1 1 0 0 1 0]'

`X = `*8×2*
1 1
1 0
0 1
1 1
1 0
0 0
0 1
1 0

Specify that the first bit in each set of four column-wise bit elements is the sign bit. Then, convert the bits to integers.

n = 4; tf = true; Y = bit2int(X,n,IsSigned=tf)

`Y = `*2×2*
-3 -5
-7 2

Convert the same bit array specifying that the first bit in each set of four column-wise bit elements is not the sign bit.

tf = false; Y = bit2int(X,n,IsSigned=tf)

`Y = `*2×2*
13 11
9 2

## Input Arguments

`X`

— Bits

column vector | matrix | array | `dlarray`

object

Bits, specified as a column vector, matrix, array, or a `dlarray`

(Deep Learning Toolbox)
object. Input bit values must be numeric or logical `0`

s and
`1`

s. For more information, see Array Support.

**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. The
number of bits, `n`

, includes the signed bit.

**Data Types: **`double`

`msbfirst`

— Specification of MSB first

`true`

or `1`

(default) | `false`

or `0`

`tf`

— Signedness of integer

`false`

or `0`

(default) | `true`

or `1`

Signedness of the integer, specified as an optional logical name-value pair,
`IsSigned = tf`

. When `tf`

is true, for each block
of `n`

bits, the
first bit is a signed bit and the output may contain negative values. If the data type
of `X`

is any of the unsigned integer types and you set
`tf`

to `true`

, then the data type of
`Y`

is the smallest signed integer type that can support the number
of input bits.

**Data Types: **`logical`

## 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 type`double`

or`logical`

, then`Y`

is of data type`double`

.If

`X`

is of data type`single`

, then`Y`

is of data type`single`

.If

`X`

is an integer data type, then the signedness of`Y`

depends on the`IsSigned`

input.If the value of

`Y`

can be contained in the same integer data type, then`Y`

is of the same data type as`X`

.If the value of

`Y`

cannot be contained in the same integer data type as`X`

, then the function sets the data type of`Y`

to the smallest integer data type that is big enough to contain its value.

## More About

### Array Support

The bit2int function supports input signals represented in a
numeric array, `dlarray`

(Deep Learning Toolbox), or
`gpuArray`

(Parallel Computing Toolbox). If inputs are specified as a
combination of `dlarray`

and `gpuArray`

, the returned
matrix is a `dlarray`

object on the GPU.

The number of batch observations (*N*_{B}) is an
optional dimension that can be added to these inputs for all supported data types.

`X`

— The input data can be a 3-D array, specified as*N*_{Sym}-by-*N*_{Chan}-by-*N*_{B}array.

*N*_{Sym} is the number of groups of `n`

column-wise
bits. *N*_{Chan} is the number of channels.

For a list of Communications Toolbox™ features that support `dlarray`

objects, see AI for Wireless.

## Extended Capabilities

### C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

### 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 R2021b**

### R2024a: Add deep learning array support

The `bit2int`

function adds support for `dlarray`

(Deep Learning Toolbox) object
processing for deep learning applications.

### R2024a: Add GPU array support

The `bit2int`

function adds support for `gpuArray`

(Parallel Computing Toolbox) object processing to run code on a graphics processing unit
(GPU).

### R2023a: Add support for signed output

`bit2int`

now supports conversion of signed binary values
(2s-complement values) to signed integer output.

The function added the `IsSigned=tf`

name-value. The value for
`tf`

is a logical, either `true`

or
`false`

to indicate the signedness of the integer. The default is
`false`

. When you set `tf`

to `true`

,
the first bit in each block of `n`

bits is
considered to be a signed bit and the output may contain negative values.

## 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)