I am trying to concatenate 4 hex bytes together and am running into some trouble.
I have a 32 bit number represented as 4 hex bytes (x
). I have another 4 bit number I taking the 2's complement as (twos_complement
) and then turning that into a hex representation (ne
). THEN I want to sub the single byte (ne
) in as the third byte of the original 32 bit number (x
). Here is what I have so far:
unsigned replace_byte(unsigned x, unsigned char b) {
unsigned new;
unsigned int twos_complement;
twos_complement = (~b) +1;
unsigned int ne = (twos_complement & 0xff);
unsigned int one = (x >> 24) & 0xff;
unsigned int two = (x >> 16) & 0xff;
unsigned int three = (x >> 8) & 0xff;
unsigned int four = x & 0xff;
printf("one 0x%x, two 0x%x, three 0x%x, four 0x%x\n",one, two, three, four);
new = (one<<24) | (two<<16) | (ne) | (four) ;
printf("new 0x%x", new);}
When I enter 11123243336 for i
and 3 as b
, I get my hex values as i = 0x96ff3948
and b = 0xfffffffd
. When I run this, I get new
as 0x96ff00fd
when im expecting 0x96fffd48
.
Any help is appreciated!
It's not really clear to me what you want to achieve but from what you're expecting my guess is that you just need to shift the ne
by 8 bits, so:
new = (one<<24) | (two<<16) | (ne<<8) | (four) ;
User contributions licensed under CC BY-SA 3.0