Search⌘ K

Semantics

Explore bitwise semantics in D programming, including union, intersection, and selective bit setting or clearing. Learn how right and left bit shifts correspond to division and multiplication by powers of two, gaining practical insights into using these operations effectively in your code.

Merely understanding how these operators work at bit-level may not be sufficient to see how they are useful in programs. The following sections describe common ways that these operators are used.

| is a union set

The | operator produces the union of the 1 bits in the two expressions.

As an extreme example, let’s consider two values that both have alternating bits set to 1. The union of these values would produce a result where all of the bits are 1:

D
import std.stdio;
void print(uint number) {
writefln(" %032b %08x %10s", number, number, number);
}
void main () {
uint lhs = 0xaaaaaaaa;
uint rhs = 0x55555555;
print(lhs);
print(rhs);
writeln("| --------------------------------");
print(lhs | rhs);
}

& is an intersection set

The & operator ...