en fr

# Library - bitfield

Library bitfield implements the constructor and methods of class bitfield for bit fields (binary numbers). Basic arithmetic operators and functions are overloaded to support expressions with the same syntax as for numbers and matrices. Contrary to integer numbers, bitfield objects have a length (between 1 and 32) and are displayed in binary.

The following statement makes available functions defined in bitfield:

```use bitfield
```

## Functions

### bitfield::beginning

First bit position in a bitfield.

#### Syntax

```use bitfield
a(...beginning...)
```

#### Description

In the index expression of a bitfield, beginning is the position of the least-significant bit, i.e. 0.

### bitfield::bitfield

Bitfield object constructor.

#### Syntax

```use bitfield
a = bitfield
a = bitfield(n)
a = bitfield(n, wordlength)
```

#### Description

bitfield(n,wordlength) creates a bitfield object initialized with the wordlength least significant bits of the nonnegative integer number n. The default value of wordlength is 32 if n is a double, an int32 or a uint32 number; 16 is n is an int16 or uint16 number; or 8 if n is an int8 or uint8 number. Without argument, bitfield gives a bit field of 32 bits 0. Like any integer number in LME, n may be written in base 2, 8, 10, or 16: 0b1100, 014, 12, and 0xc all represent the same number.

The following operators and functions may be used with bitfield arguments, with results analog to the corresponding functions of LME. Logical functions operate bitwise.

Op.FunctionOp.Function
&and ~not
==eq |or
-minus +plus
\mldivide -uminus
/mrdivide +uplus
*mtimes xor
~=ne

Indexes into bit fields are non-negative integers: 0 represents the least-significant bit, and wordlength-1 the most-significant bit. Unlike arrays, bits are not selected with logical arrays, but with other bit fields where ones represent the bits to be selected; for example a(0b1011) selects bits 0, 1 and 3. This is consistent with the way bitfield::find is defined.

#### Examples

```use bitfield
a = bitfield(123, 16)
a =
0b0000000001111011
b = ~a
b =
0b1111111110000100
b = a * 5
b =
0b0000001001100111
```

### bitfield::disp

Display a bitfield object.

#### Syntax

```use bitfield
disp(a)
```

#### Description

disp(a) displays bitfield a. It is also executed implicitly when LME displays the bitfield result of an expression which does not end with a semicolon.

### bitfield::double

Convert a bitfield object to a double number.

#### Syntax

```use bitfield
n = double(a)
```

#### Description

double(a) converts bitfield a to double number.

#### Example

```use bitfield
a = bitfield(123, 16);
double(a)
123
```

bitfield::bitfield

### bitfield::end

Last bit position in a bitfield.

#### Syntax

```use bitfield
a(...end...)
```

#### Description

In the index expression of a bitfield, end is the position of the most-significant bit, i.e. 1 less than the word length.

### bitfield::find

Find the ones in a bitfield.

#### Syntax

```use bitfield
ix = find(a)
```

#### Description

find(a) finds the bits equal to 1 in bitfield a. The result is a vector of bit positions in ascending order; the least-significant bit is number 0.

#### Example

```use bitfield
a = bitfield(123, 16)
a =
0b0000000001111011
ix = find(a)
ix =
0 1 3 4 5 6
```

### bitfield::int8bitfield::int16bitfield::int32

Convert a bitfield object to a signed integer number, with sign extension.

#### Syntax

```use bitfield
n = int8(a)
n = int16(a)
n = int32(a)
```

#### Description

int8(a), int16(a), and int32(a) convert bitfield a to an int8, int16, or int32 number respectively. If a has less bits than the target integer and the most significant bit of a is 1, sign extension is performed; i.e. the most significant bits of the result are set to 1, so that it is negative. If a has more bits than the target integer, most significant bits are ignored.

#### Example

```use bitfield
a = bitfield(9, 4);
a =
0x1001
i = int8(a)
i =
210
b = bitfield(i)
b =
0b11111001
```

### bitfield::length

Word length of a bitfield.

#### Syntax

```use bitfield
wordlength = length(a)
```

#### Description

length(a) gives the number of bits of bitfield a.

#### Example

```use bitfield
a = bitfield(123, 16);
length(a)
16
```

### bitfield::sign

Get the sign of a bitfield.

#### Syntax

```use bitfield
s = sign(a)
```

#### Description

sign(a) gets the sign of bitfield a. The result is -1 if the most-significant bit of a is 1, 0 if all bits of a are 0, or 1 otherwise.

#### Example

```use bitfield
a = bitfield(5, 3)
a =
0b101
sign(a)
-1
```

### bitfield::uint8bitfield::uint16bitfield::uint32

Convert a bitfield object to an unsigned integer number.

#### Syntax

```use bitfield
n = uint8(a)
n = uint16(a)
n = uint32(a)
```

#### Description

uint8(a), uint16(a), and uint32(a) convert bitfield a to a uint8, uint16, or uint32 number respectively. If a has more bits than the target integer, most significant bits are ignored.

#### Example

```use bitfield
a = bitfield(1234, 16);
uint8(a)
210
```