Monday, May 11, 2026

The linear algebra of bit twiddling


The earlier submit regarded on the tempering step of the Mersenne Tornado, formulating a sequence of bit operations as multiplication by a matrix mod 2. This submit will take a look at the elements extra carefully.

The theorems of linear algebra usually maintain impartial of the sphere of scalars. Usually the sphere is ℝ or ℂ, however most of fundamental linear algebra works the identical over each subject [1]. Particularly, we will do linear algebra over a finite subject, and we’re taken with probably the most finite of finite fields GF(2), the sphere with simply two parts, 0 and 1.

In GF(2), addition corresponds to XOR. We are going to denote this by ⊕ to remind us that though it’s addition, it’s not the standard addition, i.e. 1 ⊕ 1 = 0. Equally, multiplication corresponds to AND. We’ll work with 8-bit numbers to make the visuals simpler to see.

Shifting a quantity left one bit corresponds to multiplication by a matrix with 1’s under the diagonal important. Shifting left by ok bits is similar as shifting left by 1 bit ok instances, so the the matrix illustration for x << ok is the okth energy of the matrix illustration of shifting left as soon as. This matrix has 1s on the okth diagonal under the principle diagonal. Under is the matrix for shifting left two bits, x << ok.

Proper shifts are the mirror picture of left shifts. Right here’s the matrix for shifting proper two bits, x >> ok.

Shifts usually are not absolutely invertible as a result of bits both fall off the left or the proper finish. The steps within the Mersenne Tornado are invertible as a result of shifts are all the time XOR’d with the unique argument. For instance, though the perform that takes x to x >> 2 isn’t invertible, the perform that takes x to x ⊕ (x >> 2) is invertible. This operation corresponds to the matrix under.

That is an higher triangular matrix, so its determinant is the product of the diagonal parts. These are all 1s, so the determinant is 1, and the matrix is invertible.

Bitwise AND multiplies every little bit of the enter by the corresponding bit in one other quantity often called the masks. The bits aligned with a 1 are stored and the bits aligned with a 0 are cleared. This corresponds to multiplying by a diagonal matrix whose diagonal parts correspond to the bits within the masks. For instance, right here is the matrix that corresponds to taking the bitwise AND with 10100100.

Every of the steps within the Mersenne Tornado tempering course of are invertible as a result of all of them correspond to triangular matrices with all 1’s on the diagonal. For instance, the road

y ^= (y <<  7) & 0x9d2c5680 

says to shift the bits of y left 7 locations, then zero out the weather akin to 0s within the masks, then XOR the end result with y. In matrix phrases, we multiply by a decrease triangular matrix with zeros on the principle diagonal, then multiply by a diagonal matrix that zeros out among the phrases, then add the identification matrix. So the matrix akin to the road of code above is decrease triangular, with all 1s on the diagonal, so it’s invertible.

[1] Till you get to eigenvalues. Then it issues whether or not the sphere is algebraically full, which no finite subject is.

Related Articles

Latest Articles