extract a byte out of an integer

Posted on 2008-06-11
Last Modified: 2010-04-21

How do I extract a byte out of an integer. For example, I like to get the first byte of a 4-byte integer.
Question by:mycuti08
  • 4
  • 2
LVL 40

Expert Comment

ID: 21759102
>> For example, I like to get the first byte of a 4-byte integer.
Something like below...
int main()


	unsigned int dw = 0xFFEEDDCC;

	unsigned char byte0 = dw & 0xFF;

	unsigned char byte1 = (dw >> 8) & 0xFF;

	unsigned char byte2 = (dw >> 16)  & 0xFF;

	unsigned char byte3 = (dw >> 24) & 0xFF;


Open in new window

LVL 40

Expert Comment

ID: 21759137
Re: { http:#21759102 }

For each byte we need to use bitwise and (&) to mask in only 1 byte, we then just bit-shift 0, 8, 16 or 32 bits to the right so we can mask in byte 0, 1, 2 or 3 and exclude the rest. The bit-shift operators don't behave as you'd expect with signed types (they take into account the sign of the type and maintain it during shifting) so when doing bitwise arithmetic it is always best to use unsigned types.
LVL 40

Accepted Solution

evilrix earned 200 total points
ID: 21759153
You might find this tutorial helpful:
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.


Author Closing Comment

ID: 31466106
Thanks evilrix. It worked great
LVL 40

Expert Comment

ID: 21759180
>> Thanks evilrix. It worked great
Did you understand how/why it worked?

Author Comment

ID: 21759486
yes I did.
Thanks evilrix

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now