# int2bit

Convert integers to bits

Since R2021b

## Syntax

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

## Description

example

````Y = int2bit(X,n)` converts each integer element in `X` to `n` column-wise bits in `Y`, with the first bit as the most significant bit (MSB).```

example

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

## Examples

collapse all

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

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

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

collapse all

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`

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

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, then `Y` is a floating-point data type.

• If `X` is a built-in unsigned integer data type, then `Y` is of data type `uint8`.

• If `X` is a built-in signed integer data type, then `Y` is of data type `int8`.

• If `X` is of data type `double`, then `Y` is of data type `double` with `n` no larger than 53.

• If `X` is of data type `single`, then `Y` is of data type `single` with `n` no larger than 24.

## Version History

Introduced in R2021b

expand all