Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

extract a byte out of an integer

Posted on 2008-06-11
6
1,297 Views
Last Modified: 2010-04-21
Hi,

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

Expert Comment

by:evilrix
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

0
 
LVL 40

Expert Comment

by:evilrix
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.
0
 
LVL 40

Accepted Solution

by:
evilrix earned 200 total points
ID: 21759153
You might find this tutorial helpful: http://www.cprogramming.com/tutorial/bitwise_operators.html
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Closing Comment

by:mycuti08
ID: 31466106
Thanks evilrix. It worked great
0
 
LVL 40

Expert Comment

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

Author Comment

by:mycuti08
ID: 21759486
yes I did.
Thanks evilrix
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VS2015 compilation and missing DLLs 9 161
How to creat good software interface ? 1 68
c++ syntax question 9 52
No module found pypyodbc, 3 33
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

860 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