Jump to content
• ### What is your GameDev Story?

• Advertisement

# How to access the second byte in a DWORD

This topic is 4546 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

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


#### Share this post

##### Share on other sites
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.

#### Share this post

##### Share on other sites

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

#### Share this post

##### Share on other sites
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.

#### Share this post

##### Share on other sites
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

#### Share this post

##### Share on other sites
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.

#### Share this post

##### Share on other sites
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.

#### Share this post

##### Share on other sites
Quote:
 Original post by RagharNote 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]

#### Share this post

##### Share on other sites
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...

#### Share this post

##### Share on other sites
Use the macros, I think they're LOWORD(x) and HIWORD(x) if you need words which is usually how you pack it.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement
• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• ### Popular Now

• 27
• 16
• 10
• 10
• 11
• Advertisement
• ### Forum Statistics

• Total Topics
634100
• Total Posts
3015527
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!