cosmarchy
cosmarchy

Reputation: 686

Which Byte is a Bit in?

A bit of a strange one but here goes....

How can I mathematically determine which byte a certain bit is in?

For example, if I have a 32-bit word there are four 8-bit bytes. I would like to know which of these four bytes a certain bit is in. So using some random bits as an example, bit 7 is in the first byte, bit 12 is in the second, 19 is in the third and so on.

Any ideas?

Thanks

Upvotes: 1

Views: 143

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

If you number your bits and bytes from zero, dividing by eight using integer division should do the trick:

 7 / 8 = 0 // Bit #7 is in byte #0
19 / 8 = 2 // Bit #19 is in byte #2

Upvotes: 4

Related Questions