How to access the second byte in a DWORD

Started by
15 comments, last by GameDev.net 17 years, 9 months ago
I don't do any byte shifting but how do I go about accessing each byte in a 4byte alignment? I need to get access to the second byte and print that value out. So would it look like this?

DWORD a = 1000;
DWORD b = a >> 2;
fout << b << endl;



Thanks
Advertisement
The >> operator is a BIT shift operation, not a byte shift.

If you want the access bits 8-16 you therefore need to shift your DWORD right by 8 bits:

DWORD a = 1000;
DWORD b = a >> 8;

However this will also include the top 16 bits shifted as well, so you need to mask those off by using the bitwise-AND operator, thusly:

fout << (b & 0xff) << endl;

Hope this helps.
"Absorb what is useful, reject what is useless, and add what is specifically your own." - Lee Jun Fan

Shifts work on bit-basis, so

DWORD a = 1000;DWORD b = (a >> 8) & 0xff;fout << b << endl;


should produce correct result. First you shift the second byte bits to the 8 lower bits and then "and" the result with 0xff to be sure that the number carries only the lowest 8 bits.


Cheers
Well it depends on your definition of the second byte. I'll go with the second byte as stored in memory, in that case you'd have to cast it to a pointer to bytes and access it as an array, like this reinterpret_cast<LPCBYTE>(&a)[1].

The bitshifting solution has a few issues with endianess as illustrated below:

mem: 01 02 03 04

le reg: 0x04030201
be reg: 0x01020304

le: (a >> 8) & 0xff = 0x02
be: (a >> 8) & 0xff = 0x03

reinterpret_cast<LPCBYTE>(&a)[1] = 0x02 or
((LPCBYTE)&a)[1] = 0x02

Of course in your example the point is moot because you use a constant integer which will change in memory layout, thanks to the compiler, depending on endianess. If you can safely assume that the DWORD is in native endianess the shifting should work as well. The use of DWORD probably means portability isn't a big concern though.

I hope I didn't help you with some homework or something. I saw the two previous replies as I checked right before submitting but I think I add a little.
No this isn't homework... I am not in college anymore. FYI

Anyway

that works but what about the low byte now I would have to shift 24 bits correct.


So this would be correct?

DWORD a = 1000;DWORD b = (a >> 24) & 0x1000;//<- this I am unsure offout << b << endl;


Thanks
Mask is always the same, the byte gets shifted down to the lowest eight bits, then the other bits get masked out. So (a >> (byte * 8)) & 0xff where the first byte is byte 0.
3 = 11
2 = 10
1 = 0x1
2 = 0x2

11 << (0x1)
10

0xFF = 11111111

256 & 0xFF = 0
100000000
and 011111111
000000000

0xFF & 0x2 = 0x2
11111111
and 10
10

Note in more advanced programming languages, as Java, you should use >>> for shift right operator. C++ has problems because it abstracted too much.

Quote:Original post by Raghar
Note in more advanced programming languages, as Java, you should use >>> for shift right operator. C++ has problems because it abstracted too much.


What? Nevermind

[Edited by - load_bitmap_file on August 10, 2006 8:54:42 AM]
Ugh I am confused by everyone! I just need the low order byte now. The post by Demus79 and JY work. Now I just want to grab the low order byte. This is for DX I need to get a version which MS shoved major and minor into a DWORD...
Use the macros, I think they're LOWORD(x) and HIWORD(x) if you need words which is usually how you pack it.

This topic is closed to new replies.

Advertisement