bitor - Bit-wise OR - MATLAB (original) (raw)

Syntax

Description

[C](#bth5rhn-C) = bitor([A,B](#bth5rhn-AB)) returns the bit-wise OR of A and B.

example

[C](#bth5rhn-C) = bitor([A,B](#bth5rhn-AB),[assumedtype](#bth5rhn-assumedtype)) assumes that A and B are of assumedtype.

example

[objout](#bth5rhn-objout) = bitor([netobj1](#bth5rhn-netobj1),[netobj2](#bth5rhn-netobj1)) returns the bit-wise OR of the .NET enumeration objects netobj1 and netobj2.

Examples

collapse all

Create a truth table for the logical OR operation.

A = uint8([0 1; 0 1]); B = uint8([0 0; 1 1]); TTable = bitor(A, B)

TTable = 2×2 uint8 matrix

0 1 1 1

bitor returns 1 if either bit-wise input is 1.

MATLAB® encodes negative integers using two's complement. For example, to find the two's complement representation of -5, you take the bit pattern of the positive version of the number (00000101), swap each bit (11111010), and then add 1 to the result (11111011).

Therefore, the bit-wise OR of -5 (11111011) and 6 (00000110) is -1 (11111111).

a = -5; bitget(a,8:-1:1,'int8')

ans = 1×8

 1     1     1     1     1     0     1     1

b = 6; bitget(b,8:-1:1,'int8')

ans = 1×8

 0     0     0     0     0     1     1     0

ans = 1×8

 1     1     1     1     1     1     1     1

Use bitor and bitshift to pack four 8-bit bytes into the 32-bit integer they make up.

Create four bytes of data. Specify the data with hexadecimal literals, using the -u32 suffix to specify that the data should be stored as uint32. Each byte contains 8 bits worth of data.

byte4 = 0x87u32; byte3 = 0x65u32; byte2 = 0x43u32; byte1 = 0x21u32;

Start by adding the first byte as the first 8 bits of a 32-bit unsigned integer.

Next, pack the other three bytes into packedNum, using bitshift to shift the bytes to the proper locations, and bitor to copy the bits over.

packedNum = bitor(packedNum,bitshift(byte2,8)); packedNum = bitor(packedNum,bitshift(byte3,82)); packedNum = bitor(packedNum,bitshift(byte4,83));

View the packed 32-bit integer.

packedNum = uint32 87654321

Input Arguments

collapse all

Input values, specified as scalars, vectors, matrices, or multidimensional arrays. Inputs A and B must either be the same size or have sizes that are compatible (for example, A is an M-by-N matrix and B is a scalar or 1-by-N row vector). For more information, see Compatible Array Sizes for Basic Operations. A and B also must be the same data type unless one is a scalar double.

Data Types: double | logical | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Assumed data type of A and B, specified as 'uint64', 'uint32', 'uint16', 'uint8', 'int64', 'int32', 'int16', or 'int8'.

Data Types: char | string

Input values, specified as .NET enumeration objects. You must be running a version of Windows® to use .NET enumeration objects as input arguments.

bitor is an instance method for MATLAB enumeration objects created from a .NET enumeration.

Output Arguments

collapse all

Bit-wise OR result, returned as an array. C is the same data type as A and B.

Bit-wise OR result, returned as a .NET enumeration objects.

Extended Capabilities

expand all

The bitor function supports GPU array input with these usage notes and limitations:

For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Version History

Introduced before R2006a