and_bits

Definition: atom x3 = and_bits(atom x1, atom x2)
Description: Perform the logical AND operation on corresponding bits in x1 and x2.
A bit in x3 will be 1 only if the corresponding bits in x1 and x2 are both 1.

Commonly used to test bit settings in a bit-field (see example 1).
Comments: This function may be applied to an atom or sq_and_bits() to all elements of a sequence. The rules for sequence operations apply.

The arguments must be representable as 32-bit numbers, either signed or unsigned.

If you intend to manipulate full 32-bit values, you should declare your variables as atom, rather than integer. The integer type is limited to 31-bits.

Results are treated as signed numbers. They will be negative when the highest-order bit is 1.

Caution: many cryptographic functions and the like require unsigned 32-bit integers, especially when bit-shifting. Sometimes a poke4() followed by a peek4u() might help.

To understand the binary representation of a number you should display it in hexadecimal notation. Use the %x format of printf().
Example 1:
if and_bits(flags,XPAD) then width-=XPADDING end if -- apply width padding
Example 2:
a = and_bits(#0F0F0000, #12345678)
-- a is #02040000
Example 3:
s = sq_and_bits(#FF, {#123456, #876543, #2211})
-- s is {#56, #43, #11}
Example 4:
a = and_bits(#FFFFFFFF, #FFFFFFFF)
-- a is -1
-- Note that #FFFFFFFF is a positive number,
-- but the result of a bitwise logical operation is interpreted
-- as a signed 32-bit number, so it is negative.
See Also: or_bits, xor_bits, not_bits, int_to_bits