# Operators in MATLAB - Bitwise Operators

MATLAB logo

Different types of operators are used in MATLAB. We will be discussing the Bitwise operators here. It is assumed that the reader of this article has some basic knowledge of MATLAB. For a detailed discussion of arithmetic operators click on the following link.

Arithmetic Operators

## Bitwise Operators

bitand, bitor, bitxor, bitcmp, and bitshift.

Important Note: In all of the following examples the matrices A and B are supposed to be unsigned integers declared using data type uint8 unless otherwise specified.

bitand(A, B)

This command returns the bitwise-and of the two arguments A and B. For example if A=[1 0; 1 1] and B=[0 1; 1 1] then bitand(uint8(A), uint8(B)) will result in [0 0;1 1]

bitor(A, B)

This command returns the bitwise-or of the two arguments A and B. For example if A=[1 0; 1 1] and B=[0 1; 1 1] then bitor(uint8(A), uint8(B)) will result in [1 1;1 1]

bitxor(A, B)

This command returns the bitwise-exclusive or of the two arguments A and B. For example if A=[1 0; 1 1] and B=[0 1; 1 1] then bitxor(uint8(A), uint8(B)) will result in [1 1;0 0]

bitcmp(A)

This command returns the bitwise-complement of the argument A. For example if A=[1 0; 1 1] then bitcmp(uint8(A)) will result in [254 255;254 254]

Following is taken from MATLAB help.

bitshift(A, K)

C = BITSHIFT(A,K) returns the value of A shifted by K bits. A must be an unsigned integer. Shifting by K is the same as multiplication by 2^K. Negative values of K are allowed and this corresponds to shifting to the right, or dividing by 2^ABS(K) and truncating to an integer. If the shift causes C to overflow the number of bits in theunsigned integer class of A, then the overflowing bits are dropped.