C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Re: combine 4 bytes into a 32 bit number Posted by Tsag on 26 Jan 2012 at 12:21 PM
Should work . . .

I used your code and wrote it in the following way both gave correct outputs for me.

#include <iostream>

int
main(
  void)
{
  // 8-bit values
  char a = 0x56;
  char b = 0x78;
  char c = 0x9A;
  char d = 0xBC;

  // combine into a single 32-bit value
  unsigned int value = d | (c<<8) | (b<<16) | (a<<24);
  std::cout << std::hex << value << std::endl;
}


output is:

0x5678ABC

Compiled in both x64 and x86, result is the same.
--
What output are you getting?
Thread Tree
rudolfkharpuri combine 4 bytes into a 32 bit number on 25 Jan 2012 at 8:56 PM
Tsag Re: combine 4 bytes into a 32 bit number on 26 Jan 2012 at 12:21 PM
anoopakes Re: combine 4 bytes into a 32 bit number on 23 Jan 2013 at 9:26 PM



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.