Bitwise operations work directly on the binary digits of an integer. They let a market-data decoder test one flag in a packed field even when other flags are set. The data type determines how the integer is represented. The message schema determines what each bit means. FIX Simple Binary Encoding, for example, can encode several independent choices as bits of one unsigned integer. FIX SBE 1.0, §4.14
Four operations on one flag field
Take a fictional quote monitor with an unsigned four-bit field. Bit 0 means stale, bit 1 crossed, bit 2 sequence gap and bit 3 manual review. Bit 0 is the least significant bit, at the right of the written word. A mask is an integer whose bit pattern selects positions. To select bit , use
1 << k for .The starting word is
0101, so the stale and sequence-gap flags are set. Follow the updates in order:- Test for a sequence gap with AND (
&). An output bit is 1 only when both input bits are 1. AND0101with0100and the result is0100(decimal 4). Its nonzero value means the flag is set. The stored word stays0101. - Set the crossed flag with OR (
|). An output bit is 1 when at least one input bit is 1. OR0101with0010to get0111(decimal 7). - Clear the sequence-gap flag with AND. Use
1011, which has a zero at bit 2 and ones everywhere else in the four-bit field. AND0111with1011to get0011(decimal 3). - Toggle the stale flag with XOR (
^). An output bit is 1 when the two input bits differ. XOR0011with0001to get0010(decimal 2). Only the crossed flag remains set.
The zero in the mask clears bit 2. The three ones pass their input bits through.
Here are the same steps in Python:
width = 4
all_bits = (1 << width) - 1
flags = 0b0101
sequence_gap = 1 << 2
crossed = 1 << 1
stale = 1 << 0
tested = flags & sequence_gap
print(f"{tested:04b}", tested != 0)
# 0100 True
flags |= crossed
print(f"{flags:04b}")
# 0111
clear_mask = (~sequence_gap) & all_bits
flags &= clear_mask
print(f"{clear_mask:04b}", f"{flags:04b}")
# 1011 0011
flags ^= stale
print(f"{flags:04b}")
# 0010When one mask selects several flags,
(flags & mask) != 0 tests whether any selected flag is set. (flags & mask) == mask tests whether all are set. Set and clear give the same result when repeated on the same flag. A second toggle flips the flag back, which matters if the same update can be replayed.Width, signedness and byte order
Python's integer inversion
~x equals -(x + 1). In the example, ~sequence_gap is -5. AND with the all-ones mask 1111 keeps only the four bits that belong to the field. Python also allows nonnegative shift counts without imposing a field width. For instance, 0b1001 << 1 gives 10010 in binary. Masking back to four bits gives 0010 if truncation is what the schema calls for.For an unsigned field, shifting right brings in zeros on the left. In Python, shifting a negative integer right follows floor division by a power of two, so the sign matters. For a field spanning multiple bytes, decode using the schema’s byte order and signedness before applying masks. Byte order tells you how to assemble the integer from bytes. The flag definitions tell you which positions to test.
For how these conditions affect the use of a quote, see the quality-flags section of Trades, Quotes and Market State. The preview is public and the full lesson requires membership.