I have a function called replaceByte(x,n,c)
, which will replace bytes n
to x
in the c
with the following restrictions:
but when I test it, I get this error:
ERROR: Test replaceByte (-2147483648 [0x80000000], 0 [0x0], 0 [0x0]) failed ... ... Gives 0 [0x0]. Must be -2147483648 [0x80000000]
after realizing that * is not a legal operator, I finally figured it out ... and if you're interested, this is what I did:
int replaceByte(int x, int n, int c) { int mask = 0xff << (n << 3); int shift = (c << (n << 3)); return (~mask & x) | shift; }
source share