2.59 Bit Expressions
★★
Problem:
Write a C expression that will yield a word consisting of the least significant byte of x
and the remaining bytes of y
. For operands x=0x89abcdef
and y=0x76543210
, this would give 0x765432ef
.
expression:
(x & 0xff) | (y & ~0xff)
test:
#include <stdio.h>
#include <assert.h>
int main() {
size_t mask = 0xff;
size_t x = 0x89abcdef;
size_t y = 0x76543210;
size_t result = (x & mask) | (y & ~mask);
assert(result==0x765432ef);
}
Last updated
Was this helpful?