What Are 0x01 And 0x80 Representative Of In C ... - CopyProgramming

Question:

For bitwise operators homework, I'm attempting to invert the bit sequence in C. Although I discovered a solution, I'm uncertain about the hex values employed, specifically 0x01 and 0x80.

unsigned char reverse(unsigned char c) { int shift; unsigned char result = 0; for (shift = 0; shift < CHAR_BITS; shift++) { if (c & (0x01 << shift)) result |= (0x80 >> shift); } return result; }

I'm currently using a book as a reference, but it hasn't covered these specific values. As a result, I'm unsure of their significance. Would someone be able to provide some insight into this solution? Thank you in advance!

Solution 1:

The decimal value of 1 is indicated by the least significant bit being set to 0x01.

The 8-bit byte set has a most significant bit known as 0x80. On machines that use 2's-complement notation (which is common), this bit is the most negative value when stored in a signed char, with a decimal value of -128. Conversely, if stored in an unsigned char, it has a decimal value of +128.

The pattern that easily becomes instinctive is setting all bits to 1, which is equivalent to 0xFF. For signed characters, this translates to -1, while for unsigned characters, it is 255. On the other hand, there's also 0x00 or zero, which has no bits set.

During the first cycle, the loop examines the LSB to verify if it is set. If it is set, the MSB is set in the result. The loop examines the next LSB and sets the following MSB during the next cycle, and so on.

| MSB | | | | | | | LSB | | 1 | 0 | 1 | 1 | 0 | 0 | 1 | 1 | Input | 1 | 1 | 0 | 0 | 1 | 1 | 0 | 1 | Output | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0x80 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0x01 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | (0x80 >> 1) | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | (0x01 << 1)

Solution 2:

Each hex digit represents 4bits, so

  • Writing 0x01 is a lengthier format for expressing the numerical value of 1.
  • The binary representation of 128 is [1000][0000], which can also be expressed as 0x80.

Employing bitwise operators allows for both testing and setting of values.

The expression:

if (a & b) { ... }

Performs '...' only when the corresponding bit is 1 in both 'a' and 'b'.

The expression

c |= b

The function assigns a value of 1 to the bits in 'c' that are also set to 1 in 'b'.

The bit that is being tested and set is shifted along the line by the loop.

Good luck!

Solution 3:

The significance of the least significant bit and the most significant bit of the type unsigned char is emphasized by representing the values 0x01 and 0x80 in hexadecimal notation.

However, there were various errors committed by the author.

  • The CHAR_BITS macro has an error and needs to be corrected to CHAR_BIT .
  • Employing CHAR_BIT in lieu of hard-coding the widely accepted value 8 is a commendable initiative towards achieving complete portability. However, the adoption of 0x80 negates this effort, as it is only applicable under the condition that CHAR_BIT == 8 .
  • There exists a slight issue with portability where 0x01 << shift would yield undefined behavior on a platform with sizeof(unsigned char) == sizeof(int) because 0x01 is of type int instead of unsigned int , which may seem counter-intuitive.

A revised edition is available that is functional on all compliant systems.

#include unsigned char reverse(unsigned char c) { int shift; unsigned char result = 0; for (shift = 0; shift < CHAR_BIT; shift++) { result <<= 1; result |= c & 1; c >>= 1; } return result; }

Solution 4:

The code 0x01 represents the lowest bit of an eight-bit number, which is equivalent to a one in the ones place. On the other hand, 0x80 represents the highest bit of the same number, which is equivalent to an 8 in the sixteens place. By shifting these codes, masks for each bit in the byte can be obtained.

The digits in a hexadecimal number are not arranged in powers of ten but powers of sixteen. This means that the ones place is represented by the first digit from the right, such as 0x1 which equals 1. The second digit represents the sixteens place, such as 0x10 which equals 16. Similarly, the third digit represents the two-hundred-fifty-sixes place, as in 0x100 which equals 256, and so on.

Từ khóa » C 0x80