Topic: DMD0346

Help File Version: 2.10.0.636

Math Bit-wise AND Operator


The Math Bit-wise AND operator ( & ) compares each bit of its first operand to the corresponding bit of the second operand. If both bits are 1's, the corresponding bit of the result is set to 1. Otherwise, the corresponding bit of the result is set to 0.

 

Note: bit-wise AND operations are typically only used for unsigned hexadecimal values and unsigned integer memory locations. Using the :U cast operator on signed 16-bit values will cause them to be processed as unsigned 16-bit values. There is no unsigned cast operation for 32-bit signed values.

 

The operands can be any mix of signed integers, unsigned integers, real (floating point) numbers or discrete values. They can be any numeric or discrete memory location, or any numeric or discrete structure member.

 

All discrete values and all 16-bit integer values are promoted to 32-bit 2's complement signed integer values. If a Real value is included in the equation then all of the values will be promoted to Real values and all calculations will be performed using Real numbers.

 

Traditional math precedence rules are used to solve the math expression. The use of parentheses to remove any ambiguity in the processing order is encouraged.

 

Refer to the examples below:

 

Unsigned Integers: assume V0 = 0x1234 and V1 = 0x5678

 

V2 = V0 & V1

 

V0 = 0x1234 & 0x5678

 

V0 = 0001_0010_0011_0100 (0x1234)
V1 = 0101_0110_0111_1000 (0x5678)

 

V0 = 0001_0010_0011_0000 (0x1230)

 

Both operands are unsigned integer memory locations.

 

The result is placed in an unsigned integer memory location.

 

 

As stated earlier, bit-wise operations are intended for use on Unsigned integer values, NOT Signed integer values or Real values. The following examples are here only to provide understanding in how these values will be processed by the math stack, the result that is generated is typically not useful.

 

Signed Integers: assume D0 = 12345678 and N0 = 1234 and N1 = -1234

 

D1 = D0 & N0

 

D0 = 12345678 & 1234

 

D0 = 0000_0000_1011_1100_0110_0001_0100_1110 (12345678)
N0 = 0000_0000_0000_0000_0000_0100_1101_0010 (1234)

 

D1 = 0000_0000_0000_0000_0000_0000_0100_0010 (66)

 

Both operands are signed integer memory locations.

 

When the 16-bit positive value is loaded into the math stack it will be promoted to a 32-bit value by sign extending using 0's in the upper 16 bits.

 

The result is placed in a signed integer memory location.

 

D1 = D0 & N1

 

D0 = 12345678 & -1234

 

D0 = 0000_0000_1011_1100_0110_0001_0100_1110 (12345678)
N1 = 1111_1111_1111_1111_1111_1011_0010_1110 (-1234)

 

D1 = 0000_0000_1011_1100_0110_0001_0000_1110 (12345614)

 

Both operands are signed integer memory locations.

 

When the 16-bit negative value is loaded into the math stack it will be promoted to a 32-bit 2's complement value by sign extending using 1's in the upper 16 bits.

 

The result is placed in a signed integer memory location.

 

D1 = D0 & N1:U

 

D0 = 12345678 & -1234:U

 

D0 = 0000_0000_1011_1100_0110_0001_0100_1110 (12345678)
N1 = 0000_0000_0000_0000_1111_1011_0010_1110 (64302)

 

D1 = 0000_0000_0000_0000_0110_0001_0000_1110 (24846)

 

Both operands are signed integer memory locations.

 

The 16-bit negative signed value is using the :U cast operator which will interpret the 16th bit as part of the value instead of the sign bit. When it is loaded into the math stack it will be promoted to a 32-bit 2's complement value by sign extending using 0's in the upper 16 bits.

 

The result is placed in a signed integer memory location.

 

 

Real: assume D0 = 12345678 and R0 = 12345678.9

 

D1 = D0 & R0

 

D0 = 12345678 & 12345678.9

 

D0 = 0000_0000_1011_1100_0110_0001_0100_1110 (12345678)
R0 = 0000_0000_0000_0001_1110_0010_0100_0000 (123456)

 

D1 = 0000_0000_0000_0000_0110_0000_0100_0000 (24640)

 

One operand in a signed integer memory location, the other is a Real memory location.

 

When the Real value in R0 is loaded into the math stack it will be demoted to a 32-bit 2's complement value which will ignore the fractional portion of the Real value.

 

The result is placed in a signed integer memory location.

 

 


See Also:

Add

Subtract

Multiply

Divide


Modulus / Remainder

Raise to a Power


Less Than

Less than or Equal To

 

Equal To

Not Equal To

 

Greater Than

Greater Than or Equal To


Logical AND

Logical OR


Bit-wise AND

Bit-wise OR

Bit-wise XOR


Shift Left

Shift Right

Unsigned Shift Right

 

Negate

Bit-wise Invert

Logical NOT

 


Related Topics:

MATH - Calculate Expression