# bit2int

Convert bits to integers

## Syntax

``Y = bit2int(X,n)``
``Y = bit2int(X,n,msbfirst)``

## Description

example

````Y = bit2int(X,n)` converts `n` column-wise bit elements in `X` to integer values, with the first bit as the most significant bit (MSB).```

example

````Y = bit2int(X,n,msbfirst)` indicates whether the first bit in each set of `n` column-wise bits from `X` is the MSB or the least significant bit (LSB).```

## Examples

collapse all

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

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

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

collapse all

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`

Number of bits to be converted to integers, specified as a positive integer.

Data Types: `double`

Specification of MSB first, specified as a numeric or logical `1` (`true`) or `0` (`false`).

• `true` –– For each set of `n` column-wise bits in `X`, the first bit is the MSB.

• `false` –– For each set of `n` column-wise bits in `X`, the first bit is the LSB.

Data Types: `logical`

## Output Arguments

collapse all

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 and the value of `Y` can be contained in the same integer data type, then `Y` is of the same data type and signedness 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 an integer data type that is big enough to contain its value.

## Version History

Introduced in R2021b