bitand

Bit-wise AND of integers.

Category: Language Fundamentals > Operators and Elementary Operations

Syntax

C = bitand(A, B)

Bit-wise AND of the binary representations of A and B.

Description

Each bit of the result is 1 only when the corresponding bits of both operands are 1. A common use is masking: bitand(x, 15) keeps the lowest four bits of x, and bitand(x, 1) tests whether x is odd.

This operates on bits, unlike &, which treats whole values as true or false.

Input arguments

A, B
Arrays of nonnegative integers (doubles must be integer-valued, up to 2^53) or integer types of the same class. Sizes must be compatible.

Output arguments

C
Array with the combined bits, of the same class as the inputs.

Example

bitand(12, 10)
bitand(uint8([12 7]), uint8(5))
bitand(0:7, 1)
Output
ans = 8
ans = 1×2 uint8 row vector

   4   5
ans = 1×8

     0     1     0     1     0     1     0     1

See also

bitor bitxor bitshift & dec2bin