Kaiser69
Kaiser69

Reputation: 430

Split word into two byte - Theory

Just a little question about math..

I have a WORD with that value 25467. I want to get two bytes (lo / hi) from that, that is 123 and 99. There's a way to calculate that two bytes only with a calculator, avoid bitmask (&&) or shifting (<< >>).

Something like (25467 / x) - y = hi word (25467 / x) * z = lo word?

Upvotes: 2

Views: 348

Answers (3)

HelloWorld123456789
HelloWorld123456789

Reputation: 5369

upper = word/(2^8) and lower = word modulo (2^8).

Upvotes: 1

user1073719
user1073719

Reputation: 83

Assume that the WORD is organized in the order: HIBYTE LOWBYTE, then hi = (WORD)/256; low = WORD % 256

Upvotes: 0

fejese
fejese

Reputation: 4628

Shifting is equivalent with dividing with 2. So it you'd shift by 8 bit, that's dividing by 2^8:

wordValue = 25467;
hi = wordValue / 256;
low = wordValue - hi * 256

Upvotes: 0

Related Questions