Main Content

bit2int

Convert bits to integers

Since R2021b

Description

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

example

Y = bit2int(___,IsSigned=tf) specifies optional Name-Value pair 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

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 = 3×2 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 = 1×2 int8 row vector

   3   6

Specify an array of bits.

X = randi([0,1],8,2,2,'uint8') 
X = 8×2×2 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 = 2×2×2 uint8 array
Y(:,:,1) =

   13   13
    9   10


Y(:,:,2) =

    7   14
   11   10

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

collapse all

Bits, specified as a column vector, matrix, array, or a dlarray (Deep Learning Toolbox) object. Input bit values must be numeric or logical 0s and 1s. 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

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

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

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

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

collapse all

Extended Capabilities

expand all

Version History

Introduced in R2021b

expand all

See Also

Functions

Blocks