English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

MATLAB Bitwise Operations

Matlab Operators

MATLAB provides various bit operation functions, such as bitwise AND, OR, NOT, and shift operations.

The following table shows commonly used bitwise operations-

FunctionDescription
bitand(a, b)

Bitwise AND of integer a and b

bitcmp(a)

Bitwise complement of A

bitget(a,pos)

Get bit at specified position in integer array a

bitor(a, b)IntegeraAndb'sBitwise OR
bitset(a, pos)

Set bit to a specific position pos of a

bitshift(a, k)ReturnOneBy moving leftķBit, equivalent to multiplying by2 ķThe negative value of k corresponds to right shift or division by2 | k | Round to the nearest integer towards negative infinity. Any overflow bits will be truncated.
bitxor(a, b)

Bitwise XOR of Integer a and b

swapbytesSwap Byte Order

Online Example

Create a script file and enter the following code-

a = 60;                % 60 = 0011 1100   
b = 13;                % 13 = 0000 1101 
c = bitand(a, b)      % 12 = 0000 1100  
c = bitor(a, b)        % 61 = 0011 1101 
c = bitxor(a, b)      % 49 = 0011 0001 
c = bitshift(a, 2)    % 240 = 1111 0000 */
c = bitshift(a,-2)    % 15 = 0000 1111 */
When running the file, it displays the following result
c =  12
c =  61
c =  49
c =  240
c =  15

Matlab Operators