bitwise operation xor mask

I'm sorry if this seems like a stupid question I'm just having trouble understanding bits and bitwise operation assignment

I have two integers one is a mask and the other is arbitrary and I'm supposed to xor the integer with the 16 most significant bits of the mask but I'm not exactly sure if significant is the first 16 or the last 16 and if my operation is even correct since I don't know how to verify it I tried this

int main(){
    uint32_t mask = 3405705229;
    uint16_t arbitrary = 0xABCD;

    arbitrary^=mask&16;
    printf("%X\n",arbitrary);
}

I assumed mask&16 would give me only the first 16 bits of mask But when I print I still get ABCD so that can't be right.. I also tried arbitrary^=(mask>>16)&16; but that didn't do anything either


Solution 1:

For an unsigned 8-bit value, you might have:

Bit number:    7   6   5   4   3   2   1   0
Bit value:     1   0   0   0   1   1   0   1   = 141 = 0x8D

The 4 most significant bits (MSB) are bits 7-4; the 4 least significant bits (LSB) are bits 3-0.

You'd extract the 4 most significant bits from uint8_t x8 = 141; using:

uint8_t y = (x >> 4) & 0xF

The output would be y equal to 8 or 0x08.

You can, of course, expand this to accommodate larger numbers of bits.

Note that MSB and LSB are often used for {Most|Least} Significant Bytes rather than bits — but that still applies in a similar way to the logical value in chunks of 8-bit bytes. As paulsm4 noted in a comment, this applies to the logical value. CPUs come in two main flavours: big-endian (SPARC, older PowerPC, Motorola, …) and little-endian (Intel and many other chips, including modern PowerPC at least as an option). The difference is the order in which the bytes of a multi-byte (integer) value are stored:

For a 4-byte unsigned integer with the value 0x12345678, the two types store the data in opposite orders:

Address 0x1000 0x1001 0x1002 0x1003 Big-Endian 0x12 0x34 0x56 0x78 Little-Endian 0x78 0x56 0x34 0x12

Big-Endian MSB ... ... LSB Little-Endian LSB ... ... MSB

These days, little-endian is more widespread. However, many network protocols and other systems mandate big-endian. Most of the time, you don't have to worry about it. Sometimes, you do — and it is important to know when you do and when you don't. Data on a single machine, not shared elsewhere, usually doesn't require you to worry about byte order. You don't have to worry about the byte order of data in (single-byte) character strings.

Solution 2:

In a binary number, the bit furthest to the left is called the most significant bit (msb) and the bit furthest to the right is called the least significant bit (lsb). if someone says to mask with 16 most significant bits then you should try to get the 16 bits from left to right.

you can get the most significant 16 bits with the help of left shift operator.

Left shift operators shift the number to the left and fill the most significant bits by zero(you should not use left shift operator with negative numbers).

so following line will do the job.

arbitrary^=(mask>>16);