Skip to content
Bit Operations Refresher
step 1/5

Reading — step 1 of 5

Read

~1 min readBits, Bytes & Endianness

Bit Operations Refresher

SHA-256 is a flurry of bit operations on 32-bit unsigned integers.

OpSymbolWhat it does
ANDa & bbits set in both
OR`ab`
XORa ^ bbits differing
NOT~aflip all bits
Left shifta << nshift bits left, fill with 0s
Right shifta >> nshift bits right, fill with 0s
Rotate rightROTR(a,n)bits that fall off the right come around

In SHA-256, ROTR is critical:

ROTR(x, n) = (x >> n) | (x << (32 - n))    (mod 2^32)

Two derived functions used in the spec:

Σ0(x) = ROTR(x,2)  ^ ROTR(x,13) ^ ROTR(x,22)
Σ1(x) = ROTR(x,6)  ^ ROTR(x,11) ^ ROTR(x,25)
σ0(x) = ROTR(x,7)  ^ ROTR(x,18) ^ (x >> 3)
σ1(x) = ROTR(x,17) ^ ROTR(x,19) ^ (x >> 10)

Plus two "majority/choice" functions:

Ch(x,y,z)  = (x & y) ^ (~x & z)        # if x then y else z, bitwise
Maj(x,y,z) = (x & y) ^ (x & z) ^ (y & z)  # majority of three bits

All arithmetic is mod 2^32 — wrap on overflow.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…