0

What are the real world uses of manipulating (clearing/setting/toggling) the MSB or LSB?

By definition, MSB is the left most bit, contributing the maximum value and LSB is the right most bit, contributing the least value.

Why one has to manipulate these bits? What can we achieve by manipulating these bits?

Prasath Govind
  • 720
  • 2
  • 10
  • 30

3 Answers3

1

If you're using an integer value as a flags structure or to contain bitfields, then that's a reason. A reason to adjust the MSB or LSB individually might be to set a special flag where you know the bit would be otherwise unused, for example in some ISAs all memory addresses (for loading/writing) must be aligned on a word boundary (typically a word length is 32-bits) which means the last few bits of a pointer are completely insignificant and can be used by application or system, the same applies to the upper bits - but only in certain circumstances.

Other reasons include doing quick arithmetic operations on IEEE-754 numbers: e.g. toggling the sign-bit which would be quicker than going through the FPU.

Dai
  • 141,631
  • 28
  • 261
  • 374
1

From Wikipedia:
MSB
Signed magnitude representation

This representation is also called "sign–magnitude" or "sign and magnitude" representation. In this approach, the problem of representing a number's sign can be to allocate one sign bit to represent the sign: setting that bit (often the most significant bit) to 0 is for a positive number or positive zero, and setting it to 1 is for a negative number or negative zero. The remaining bits in the number indicate the magnitude (or absolute value). Hence, in a byte with only seven bits (apart from the sign bit), the magnitude can range from 0000000 (0) to 1111111 (127). Thus numbers ranging from −12710 to +12710 can represented once the sign bit (the eighth bit) is added. A consequence of this representation is that there are two ways to represent zero, 00000000 (0) and 10000000 (−0). This way, −4310 encoded in an eight-bit byte is 10101011.

LSB
The least significant bits have the useful property of changing rapidly if the number changes even slightly. For example, if 1 (binary 00000001) is added to 3 (binary 00000011), the result will be 4 (binary 00000100) and three of the least significant bits will change (011 to 100). By contrast, the three most significant bits (MSBs) stay unchanged (000 to 000).

Least significant bits are frequently employed in pseudorandom number generators, hash functions and checksums.

Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60
1

One real world example of:

  • manipulating the LSB is Fenwick Tree
    can be used to find the sum of nos. in range & updation of a number in array both in O(log N)

  • manipulating the MSB is Binary Search
    using bit manipulation -- Binary searching via bitmasking?

Community
  • 1
  • 1
Anupam Ghosh
  • 314
  • 3
  • 10