DEV Community

csm-yujinkim
csm-yujinkim

Posted on

Review of MSB, LSB, and Unsigned Integers

In this article, we deal with the first two of the following key skills that build up to understanding the floating point system:

  1. Define and identify the most and least significant bits in a word. Count how many combinations are possible in a word given its bit length.
  2. Encode a positive integer in binary. Decode an unsigned integer to decimal.
  3. Encode an integer in two's complement notation. Decode a signed integer to decimal.
  4. Use sign extension to expand the number of bits in a word. Explain the mathematics.
  5. Convert a decimal fraction to binary. Explain why this does not require any particular encoding scheme, such as a floating point standard.

The MSB and the LSB. Unsigned Representation Practice

Definition: The most significant bit (MSB) is, in unsigned representation, the bit position associated with the greatest absolute value. Also, in two's complement representation, the bit position that changes the sign of a number.

Definition: The least significant bit (LSB) is the bit position that has the least value associated, that is, the value of 1.

Let's say we have a 32-bit machine that deals with 32-bit words. Each word is a string of four octets (bytes), so there are 32 bits in it. Then, generally, bit position 0 is the LSB, and bit position 31 is the MSB.

Bit position n has the value of 2^n, so, for instance, the number 1010 (unsigned) will be equal to 2^3 + 2^1 = 8 + 1 = 9. In this instance, the rightmost bit is the LSB and the leftmost bit the MSB.

Number of Combinations In a Word That Has n Bits

In unsigned notation like this, there are a total of 2^n combinations of the digits where n is the number of bits in a word. If there are 32 bits in a word, then 2^32 numbers can be represented. If we made a sequence of these numbers, it will start from 0, so the last number is exactly 1 less than the number of combinations, that is, 2^n - 1. So, in unsigned notation, numbers [0, 2^n - 1] are represented.

Practice

QUESTION 1 Represent the decimal number 254 in unsigned binary notation. What are the most and least significant bits?

ANSWER: The decimal number 254 is closest to a smaller (or equal) power of two, 128, or 2^7 . Divide 254 with that power. (Since the quotient is always 1, we replace the division by subtraction.) The remainder is 126. Do the same: The closest smaller (or equal) power of two is 64, or 2^6 . The difference is 62. With 2^5 = 32, the difference is 30. With 2^4 = 16, the difference is 14. With 2^3 = 8, the difference is 6. With 2^2 = 4, the difference is 2. With the equal power of two, 2^1 = 2, the difference is 0. The exponents of two are where the 1's are. They are bit positions 1, 2, 3, 4, 5, 6 and 7.

Let's use an octet (an eight-bit string). The leftmost is the MSB, so it is bit position 7. The rightmost is the LSB, and its bit position is 0. Now, arrange it: 1111 1110. This is our representation.

The MSB is 1, and the LSB is 0.

Let's use a 32-bit word. Again, the leftmost is the MSB, and its bit position is 31. The rightmost is the LSB, and its position is 0. Now, arrange it: 0000 0000 0000 0000 0000 0000 1111 1110. The MSB is changed: It is now 0. The LSB is still 0.

QUESTION 2 Represent 142 in unsigned representation.

Answer: Remember to choose the closest smaller (or equal) power of two, until the remainder (or difference) is 0. Then, record the bit positions---that's our method.

142 - 128 = 14

14 - 8 = 6

6 - 4 = 2

2 - 2 = 0

The powers are: 1, 2, 3, 7. Use an octet. The leftmost is the MSB, bit position 7, and the rightmost is the LSB, bit position 0. Arrange: 1000 1110.

QUESTION 3 Decode 1001 1010 in unsigned rep. to decimal. The leftmost is the MSB.

Answer: It is a sum of powers of two. The 7th, 4th, 3rd and 1st powers are represented. So, add them up: 2^7 + 2^4 + 2^3 + 2^1 = 154.

Challenge

Challenge Problem 1

Convert the positive integer 1,257,281 to binary:

  1. Is a byte sufficient to store the representation? A 2-byte word? A 32-bit word? A 64-bit word?

Challenge Problem 2

Consider the representation 1111 1111 in unsigned notation in a byte:

  1. What is its decimal value?
  2. If the most significant bit was assigned the value of -2^7 , instead of 2^7 ,then what is its new decimal value?

Top comments (0)