bitset

Set bits at certain positions

Syntax

• `c = bitset(a, bit)` example
• `c = bitset(a, bit, v)` example

Description

example

````c = bitset(a, bit)` returns the value of `a` with position `bit` set to `1` (on).```

example

````c = bitset(a, bit, v)` returns the value of `a` with position `bit` set to `v`. ```

Examples

collapse all

Set the Bit at a Certain Position

Begin with an unsigned fixed-point `fi` number with a value of 5, word length 4, and fraction length 0.

```a = fi(5,0,4,0); disp(bin(a)) ```
```0101 ```

Set the bit at position 4 to `1` (on).

```c = bitset(a,4); disp(bin(c)) ```
```1101 ```

Set the Bit at a Certain Position in a Vector

Consider the following fixed-point vector with word length 4 and fraction length 0.

```a = fi([0 1 8 2 4],0,4,0); disp(bin(a)) ```
```0000 0001 1000 0010 0100 ```

In each element of vector `a`, set the bits at position 2 to `1`.

```c = bitset(a,2,1); disp(bin(c)) ```
```0010 0011 1010 0010 0110 ```

Set the Bit at a Certain Position with Fixed Point Index

Consider the following fixed-point scalar with a value of 5.

```a = fi(5,0,4,0); disp(bin(a)) ```
```0101 ```

Set the bit at position `fi(2)` to `1`.

```c = bitset(a,fi(2),1); disp(bin(c)) ```
```0111 ```

Set the Bit When Index Is a Vector

Create a `fi` object with a value of `pi`.

```a = fi(pi); disp(bin(a)) ```
```0110010010001000 ```

In this case, `a` is signed with a word length of 16.

Create a vector of the bit positions in `a` that you want to set to on. Then, get the binary representation of the resulting `fi` vector.

```bit = fi([15,3,8,2]); c = bitset(a,bit); disp(bin(c)) ```
```0110010010001000 0110010010001100 0110010010001000 0110010010001010 ```

Input Arguments

collapse all

`a` — Input arrayscalar | vector | matrix | multidimensional array

Input array, specified as a scalar, vector, matrix, or multidimensional array of fixed-point `fi` objects. If `a` has a signed `numerictype`, the bit representation of the stored integer is in two's complement representation.

Data Types: fixed-point `fi`

`bit` — Bit indexscalar | vector | matrix | multidimensional array

Bit index, specified as a scalar, vector, matrix, or multidimensional array of `fi` objects or built-in data types. `bit` must be a number between `1` and the word length of `a`, inclusive. The `LSB` (right-most bit) is specified by bit index `1` and the `MSB` (left-most bit) is specified by the word length of `a`.

```a = fi(pi,0,8); a.bin```
`11001001`

Data Types: `fi`|`single` | `double` | `int8` | `int16` | `int32` | `int64` | `uint8` | `uint16` | `uint32` | `uint64`

`v` — Bit valuescalar | vector | matrix | multidimensional array

Bit value of `a` at index `bit`, specified as a scalar, vector, matrix, or multidimensional array of `fi` objects or built-in data types. `v` can have values of `0`, or `1`. Any value other than `0` is automatically set to `1`. When `v` is nonscalar, it must have the same dimensions as one of the other inputs.

Data Types: `fi`|`single` | `double` | `int8` | `int16` | `int32` | `int64` | `uint8` | `uint16` | `uint32` | `uint64`

Output Arguments

collapse all

`c` — Output arrayscalar | vector | matrix | multidimensional array

Output array, specified as a scalar, vector, matrix, or multidimensional array of `fi` objects.