Hacker News new | ask | show | jobs
by grimlck 4126 days ago
How does [0xFF, 0xFF, 0xFF, 0xFF], interpreted as a UInt32 turn into 16777215?

I would have guessed 4294967295

3 comments

I assume it's just a typo/incomplete edit; his graphic directly below has the bit representation 00000000111111111111111111111111 indicating [0x00, 0xFF, 0xFF, 0xFF].
Yep (credit to bkirwi):

    λ> import Data.Bits
    λ> let integerWithBytes = foldr (\byte acc -> (acc `shiftL` 8) + fromIntegral byte) 0
    λ> integerWithBytes [0xFF, 0xFF, 0xFF, 0xFF]
    4294967295