Welcome to the new platform of Programmers Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use it's exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.
combine 4 bytes into a 32 bit number
I have 4 numbers:b1 b2 b3 b4 (b1 is MSB, b4 is LSB) and I want to combine them into one number of 32-bit as b1b2b3b4;tried the following but didn't work (i guess overflow):
res = (unsigned long)((b4 & 0xFF) | (((unsigned long)(b3<<8)) & 0xFF00));
res = (unsigned long)(res | (((unsigned long)(b2<<16)) & 0xFF0000));
res = (unsigned long)(res |(((unsigned long)(b1<<24)) & 0xFF000000));
b1 = 86; b2 = 120; b3 = 154; b4 = 188;
result should be : 1450744508 in decimal or 56789abc in hex