Msb и lsb что это

от admin

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.

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.

TrainingMontage

This post is the 3rd in a series about basic concepts in programming and Computer Science. You can find all the posts in the series below.

  1. CS Basics: Binary Conversion
  2. CS Basics: Hex and 32 Bit Conversion
  3. CS Basics: Detour — Nibbles, MSB, and LSB
  4. CS Basics: Converting Between Different Bases

Up until now, I’ve been representing binary, quaternary, octal, and hexadecimal numbers a little incorrectly. I did this so we wouldn’t be dealing with too many concepts at once. So, let’s take a detour and look at the way binary numbers are traditionally represented.

Binary numbers are, at least in computer science, represented in groups of what’s called a nibble. A nibble is half of a byte. Because a byte is 8 bits, a nibble is 4 bits.

Each digit of a binary number represents a bit, but we don’t really represent binary numbers with a single digit. Instead, we represent all digits of a binary number in groups of 4 bits.

Since each bit can hold a maxinum of two values, a nibble can represent a range of 16 values from 0 — 15.

A Table of Nibbles

N2 Nibble
0 0 0 0 0
1 0 0 0 1
2 0 0 1 0
3 0 0 1 1
4 0 1 0 0
5 0 1 0 1
6 0 1 1 0
7 0 1 1 1
8 1 0 0 0
9 1 0 0 1
10 1 0 1 0
11 1 0 1 1
12 1 1 0 0
13 1 1 0 1
14 1 1 1 0
15 1 1 1 1

So, now that we know binary numbers are represented in nibbles, we can talk about the Least Significant Bit, or LSB, and the Most Significant Bit, or MSB.

For binary integers, the LSB is the bit position that gives the units value. If this were decimal, that would be the ones.

The MSB is the bit position that gives the highest value for that binary integer.

N10 MSB LSB
10 1 0 1 0
11 1 0 1 1

The integers in the table above are, of course, only showing 4 bit numbers. The principle still stands for larger 1 byte numbers, though.

N10 MSB LSB
150 1 0 0 1 0 1 1 0

Usually the LSB is shown at the first position, in our case that’s all the way to the right. The MSB is shown in the last position, or all the way on the left. There are some differences in the way some processors have handled this but we don’t need to get into that for this post.

That’s it for nibbles, LSB, and MSB! We can get into converting between different bases on the next post.

After that, I’ll write about using bitwise operators with our different numbering systems so we can take full advantage of our newfound knowledge.

What is the use of MSB and LSB? [duplicate]

I have a hard time understanding the use of MSB (Most significant bit) vs. LSB (Least significant bit) and why they’re used at all. It is my understanding that it has to do with Endianness, but I cannot get my head around it.

Basically: If I read a specification on X and they specify that the data needs to be read LSB or MSB — ok I can do that — but why am I doing it.

Why don’t we just send all data MSB?

4 Answers 4

Endianness for binary (base 2) is no different than endianness for any other base.

Without a specification telling you which side the most significant and least significant decimals were, you wouldn’t know whether

is one-thousand-two-hundred-and-thirty-four or four-thousand-three-hundred-and-twenty-one.

Note for additional curiosity that «twenty-one» is MSD whereas «nineteen» is LSD, so it’s certainly not that universally obvious even within the same community which one is the right one.

For dates, there are three different conventions (using Alan Turing’s birthday as example):

  • least significant component first (e.g. in Germany): 23.06.12
  • most significant component first (e.g. ISO): 12-06-23
  • complete and utter confusion (US): 06/23/12

Again, you have to pick some order, and you have to communicate which order you picked, and there is no obviously «right» order. (I would argue though, that the US date format is obviously wrong 😀 .)

When a byte is serialized into a stream of bits and transmitted serially, it becomes important to know whether it’s transmitted LSbit-first or MSbit-first. The transmitter can send the bits in either order, and the contract whether it’s LSbit-first or MSbit-first is established in the spec (or datasheet). For example, the receiver receives this:

If the transmitter was sending LSbit-first, then the value is 0x02 . If the transmitter was sending MSbit-first, then the value is 0x40 . The receive has to know which one it is.

Bit order and byte order are two separate things, but they may affect your preferences. Say you have a little-endian machine, and you want to transmit a 32-bit number. Also transmitting the bits LSB-first means overall bit order for the entire int (with lsb numbered 0) is:

If you transmitted MSB-first, the overall bit order for the entire int would be:

Looking at this signal on an oscilloscope just got more difficult, and designing shift registers in hardware just got a little more tricky.

The reverse occurs for big-endian machines. So endianness doesn’t dictate bit order, but it sure does make one bit order easier to work with.

As far as preferring one endianness over another, it’s more than picking one at random. Big endian is easier to understand conceptually, but little endian means you don’t have to offset the address in order to treat a byte as a word.

Karl Bielefeldt's user avatar

MSB and LSB can be thought of in terms of numeric properties of bit sequences. For example, during addition, the carries flow from the addition of two LSB’s toward the next higher bit. The LSB itself receives no carry because it starts the addition; whereas all the other bits get a carry from the next least significant bit position. Overflow is when a carry (of value 1) happens from the MSB, because there are no more bits (in the byte or word size) left to carry over to. The MSB is also considered the sign bit for signed data types: if the MSB is 1 the value is negative, if 0 the value is positive (or zero).

Endianess is a matter of the storage (or transmission) order for bits within a byte and also more significantly (as it turns out) for the order of bytes within a word or long word. These differences can be seen when data is moved from one system to another. Fortunately bit order within a byte is standardized on hard drives and networks, so we don’t have to worry about bit endianess. However, byte order within a word is different for some processors than others, and bytes become swapped when changing architectures; as this causes problems it requires mitigation (e.g. software has to handle it). Little endian stores least significant bytes first followed by more significant bytes in higher storage or packet address order, whereas big endian is the opposite.

    The Overflow Blog
Linked
Related
Hot Network Questions

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.26.43546

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

What is MSB first and LSB first?

As with bits, the MSB (byte) is normally the byte farthest to the left, or the byte transmitted first in a sequence. When the MSB in a sequence is farthest to the left (or first), the least significant bit or byte (LSB) is usually the one farthest to the right (or last).

What is the meaning of MSB?

Most-significant bit
Most-significant bit. In a binary number, the MSB is the most weighted bit in the number. Typically, binary numbers are written with the MSB in the left-most position; the LSB is the furthest-right bit.

Is I2C MSB or LSB first?

The first seven bits, which follow after START condition, contain I2C slave address. As any other data, the address is transmitted sequentially, starting with the most significant bit (MSB) and ending with the least significant bit (LSB).

What is big and little endian?

Big-endian is an order in which the “big end” (most significant value in the sequence) is stored first, at the lowest storage address. Little-endian is an order in which the “little end” (least significant value in the sequence) is stored first.

What is the binary number of 4?

0100
Hexadecimal Numbers

Decimal Number 4-bit Binary Number Hexadecimal Number
4 0100 4
5 0101 5
6 0110 6
7 0111 7

What are bits used for?

A bit (short for binary digit) is the smallest unit of data in a computer. A bit has a single binary value, either 0 or 1. Although computers usually provide instructions that can test and manipulate bits, they generally are designed to store data and execute instructions in bit multiples called bytes.

What is the full meaning of LSB?

The full form of LSB is Least Significant Bit. Sometimes abbreviated as LSB, the least significant bit is the lowest bit in a series of numbers in binary. It is either the leftmost or rightmost bit in a binary number, depending on the computer’s architecture. A string of 8 bits is called a byte.

How do you calculate MSB and LSB?

MSB stands for most significant bit, while LSB is least significant bit. In binary terms, the MSB is the bit that has the greatest effect on the number, and it is the left-most bit. For example, for a binary number 0011 0101, the Most Significant 4 bits would be 0011. The Least Significant 4 bits would be 0101.

What is SDA and SCL in I2C?

SCL is the clock line. It is used to synchronize all data transfers over the I2C bus. SDA is the data line. The SCL & SDA lines are connected to all devices on the I2C bus. There needs to be a third wire which is just the ground or 0 volts.

Is SPI a MSB or LSB?

The SPI protocol does not specify whether the most-significant bit (MSb) or least-significant bit (LSb) is transmitted or received first during an SPI transfer. Hence, most SPI devices allow this bit order to be configured.

What is byte ordering?

Byte order refers to the order of digits in computer words at least 16 bits long. See word. Big Endian and Little Endian. Big endian is how we normally deal with numbers: the most significant byte or digits are placed leftmost in the structure (the big end).

What is the difference between MSB and LSB?

MSB stands for Most Significant Bit. And LSB stands for Least Significant Bit. For an integer expressed with b bits, the LSB is the “ 1 ” bit, and the MSB is the “ 2 b − 1 ” bit (or the sign bit in two’s complement representation).

What is the location of MSB and LSB in unsigned integers?

Unsigned integer example. This table illustrates an example of decimal value of 149 and the location of LSB. In this particular example, the position of unit value (decimal 1 or 0) is located in bit position 0 (n = 0). MSB stands for Most Significant Bit, while LSB stands for Least Significant Bit.

What is the most significant bit (MSB)?

The most significant bit (MSB) is the bit in a multiple-bit binary number with the largest value.

What is an MSB (byte)?

The most significant byte, also abbreviated MSB, is the byte in a multiple-byte word with the largest value. As with bits, the MSB (byte) is normally the byte farthest to the left, or the byte transmitted first in a sequence.

Читать:
Как сделать дистанционный выключатель

Похожие публикации