en fr

# Logical Functions

### all

Check whether all the elements are true.

#### Syntax

```v = all(A)
v = all(A,dim)
b = all(v)
```

#### Description

all(A) performs a logical AND on the elements of the columns of array A, or the elements of a vector. If a second argument dim is provided, the operation is performed along that dimension.

all can be omitted if its result is used by if or while, because these statements consider an array to be true if all its elements are nonzero.

#### Examples

```all([1,2,3] == 2)
false
all([1,2,3] > 0)
true
```

### any

Check whether any element is true.

#### Syntax

```v = any(A)
v = any(A,dim)
b = any(v)
```

#### Description

any(A) performs a logical OR on the elements of the columns of array A, or the elements of a vector. If a second argument dim is provided, the operation is performed along that dimension.

#### Examples

```any([1,2,3] == 2)
true
any([1,2,3] > 5)
false
```

### bitall

Check whether all the corresponding bits are true.

#### Syntax

```v = bitall(A)
v = bitall(A,dim)
b = bitall(v)
```

#### Description

bitall(A) performs a bitwise AND on the elements of the columns of array A, or the elements of a vector. If a second argument dim is provided, the operation is performed along that dimension. A can be a double or an integer array. For double arrays, bitall uses the 32 least-significant bits.

#### Examples

```bitall([5, 3])
1
bitall([7uint8, 6uint8; 3uint8, 6uint8], 2)
2x1 uint8 array
6
2
```

### bitand

Bitwise AND.

#### Syntax

```c = bitand(a, b)
```

#### Description

Each bit of the result is the binary AND of the corresponding bits of the inputs.

The inputs can be scalar, arrays of the same size, or a scalar and an array. If the input arguments are of type double, so is the result, and the operation is performed on 32 bits.

#### Examples

```bitand(1,3)
1
bitand(1:6,1)
1 0 1 0 1 0
bitand(7uint8, 1234int16)
2int16
```

### bitany

Check whether any of the corresponding bits is true.

#### Syntax

```v = bitany(A)
v = bitany(A,dim)
b = bitany(v)
```

#### Description

bitany(A) performs a bitwise OR on the elements of the columns of array A, or the elements of a vector. If a second argument dim is provided, the operation is performed along that dimension. A can be a double or an integer array. For double arrays, bitany uses the 32 least-significant bits.

#### Examples

```bitany([5, 3])
7
bitany([0uint8, 6uint8; 3uint8, 6uint8], 2)
2x1 uint8 array
6
7
```

### bitcmp

Bit complement (bitwise NOT).

#### Syntax

```b = bitcmp(i)
b = bitcmp(a, n)
```

#### Description

bitcmp(i) gives the 1-complement (bitwise NOT) of the integer i.

bitcmp(a,n), where a is an integer or a double, gives the 1-complement of the n least-significant bits. The result has the same type as a.

The inputs can be scalar, arrays of the same size, or a scalar and an array. If a is of type double, so is the result, and the operation is performed on at most 32 bits.

#### Examples

```bitcmp(1,4)
14
bitcmp(0, 1:8)
1 3 7 15 31 63 127 255
bitcmp([0uint8, 1uint8, 255uint8])
1x3 uint8 array
255 254   0
```

### bitget

Bit extraction.

#### Syntax

```b = bitget(a, n)
```

#### Description

bitget(a, n) gives the n:th bit of integer a. a can be an integer or a double. The result has the same type as a. n=1 corresponds to the least significant bit.

The inputs can be scalar, arrays of the same size, or a scalar and an array. If a is of type double, so is the result, and n is limited to 32.

#### Examples

```bitget(123,5)
1
bitget(7, 1:8)
1 1 1 0 0 0 0 0
bitget(5uint8, 2)
0uint8
```

### bitor

Bitwise OR.

#### Syntax

```c = bitor(a, b)
```

#### Description

The input arguments are converted to 32-bit unsigned integers; each bit of the result is the binary OR of the corresponding bits of the inputs.

The inputs can be scalar, arrays of the same size, or a scalar and an array. If the input arguments are of type double, so is the result, and the operation is performed on 32 bits.

#### Examples

```bitor(1,2)
3
bitor(1:6,1)
1 3 3 5 5 7
bitor(7uint8, 1234int16)
1239int16
```

### bitset

Bit assignment.

#### Syntax

```b = bitset(a, n)
b = bitset(a, n, v)
```

#### Description

bitset(a,n) sets the n:th bit of integer a to 1. a can be an integer or a double. The result has the same type as a. n=1 corresponds to the least significant bit. With 3 input arguments, bitset(a,n,v) sets the bit to 1 if v is nonzero, or clears it if v is zero.

The inputs can be scalar, arrays of the same size, or a mix of them. If a is of type double, so is the result, and n is limited to 32.

#### Examples

```bitset(123,10)
635
bitset(123, 1, 0)
122
bitset(7uint8, 1:8)
1x8 uint8 array
7   7   7  15  23  39  71 135
```

### bitshift

Bit shift.

#### Syntax

```b = bitshift(a, shift)
b = bitshift(a, shift, n)
```

#### Description

The first input argument is converted to a 32-bit unsigned integer, and shifted by shift bits, to the left if shift is positive or to the right if it is negative. With a third argument n, only n bits are retained.

The inputs can be scalar, arrays of the same size, or a mix of both.

#### Examples

```bitshift(1,3)
8
bitshift(8, -2:2)
2 4 8 16 32
bitshift(15, 0:3, 4)
15 14 12 8
```

bitget

### bitxor

Bitwise exclusive OR.

#### Syntax

```c = bitxor(a, b)
```

#### Description

The input arguments are converted to 32-bit unsigned integers; each bit of the result is the binary exclusive OR of the corresponding bits of the inputs.

The inputs can be scalar, arrays of the same size, or a scalar and an array.

#### Examples

```bitxor(1,3)
2
bitxor(1:6,1)
0 3 2 5 4 7
bitxor(7uint8, 1234int16)
1237int16
```

### false

Boolean constant false.

#### Syntax

```b = false
B = false(n)
B = false(n1, n2, ...)
B = false([n1, n2, ...])
```

#### Description

The boolean constant false can be used to set the value of a variable. It is equivalent to logical(0). The constant 0 is equivalent in many cases; indices (to get or set the elements of an array) are an important exception.

With input arguments, false builds a logical array whose elements are false. The size of the array is specified by one integer for a square matrix, or several integers (either as separate arguments or in a vector) for an array of any size.

```false
false
islogical(false)
true
false(2,3)
F F F
F F F
```

### graycode

Conversion to Gray code.

#### Syntax

```g = graycode(n)
```

#### Description

graycode(n) converts the integer number n to Gray code. The argument n can be an integer number of class double (converted to an unsigned integer) or any integer type. If it is an array, conversion is performed on each element. The result has the same type and size as the input.

Gray code is an encoding which maps each integer of s bits to another integer of s bits, such that two consecutive codes (i.e. graycode(n) and graycode(n+1) for any n) have only one bit which differs.

#### Example

```graycode(0:7)
0 1 3 2 6 7 5 4
```

igraycode

### igraycode

Conversion from Gray code.

#### Syntax

```n = igraycode(g)
```

#### Description

igraycode(n) converts the Gray code g to the corresponding integer. It is the inverse of graycode. The argument n can be an integer number of class double (converted to an unsigned integer) or any integer type. If it is an array, conversion is performed on each element. The result has the same type and size as the input.

#### Example

```igraycode(graycode(0:7))
0 1 2 3 4 5 6 7
```

graycode

### islogical

Test for a boolean object.

#### Syntax

```b = islogical(obj)
```

#### Description

islogical(obj) is true if obj is a logical value, and false otherwise. The result is always a scalar, even if obj is an array. Logical values are obtained with comparison operators, logical operators, test functions, and the function logical.

#### Examples

```islogical(eye(10))
false
islogical(~eye(10))
true
```

### logical

Transform a number into a boolean.

#### Syntax

```B = logical(A)
```

#### Description

logical(x) converts array or number A to logical (boolean) type. All nonzero elements of A are converted to true, and zero elements to false.

Logical values are stored as 0 for false or 1 for true in unsigned 8-bit integers. They differ from the uint8 type when they are used to select the elements of an array or list.

#### Examples

```a=1:3; a([1,0,1])
Index out of range
a=1:3; a(logical([1,0,1]))
1 3
```

### true

Boolean constant true.

#### Syntax

```b = true
B = true(n)
B = true(n1, n2, ...)
B = true([n1, n2, ...])
```

#### Description

The boolean constant true can be used to set the value of a variable. It is equivalent to logical(1). The constant 1 is equivalent in many cases; indices (to get or set the elements of an array) are an important exception.

With input arguments, true builds a logical array whose elements are true. The size of the array is specified by one integer for a square matrix, or several integers (either as separate arguments or in a vector) for an array of any size.

```true
true
islogical(true)
true
true(2)
T T
T T
```

### xor

Exclusive or.

#### Syntax

```b3 = xor(b1,b2)
```

#### Description

xor(b1,b2) performs the exclusive or operation between the corresponding elements of b1 and b2. b1 and b2 must have the same size or one of them must be a scalar.

#### Examples

```xor([false false true true],[false true false true])
F T T F
xor(pi,8)
false
```