-3

Is there a way to check, programmatically and preferably portably, if the left bit is the most significant bit?

Dervin Thunk
  • 19,515
  • 28
  • 127
  • 217
  • How does a computer define what is "left"? – Dan F Aug 22 '13 at 13:48
  • Wait, the left bit of what? – Fabio A. Correa Aug 22 '13 at 13:50
  • @FabioA.Correa: Wow, dude, endiannes is about bytes, not bits... there may be a relation, but it's certainly not the same. – Dervin Thunk Aug 22 '13 at 13:52
  • You're right, so you're asking, the left bit of what? – Fabio A. Correa Aug 22 '13 at 13:52
  • 1
    @DervinThunk: Endianness is an issue whenever any sequence may be viewed in more than one order (e.g., ordered by address versus ordered by appearance in a group, such as a 32-bit “word”). In computing, it is encountered most often with bytes, but it may occur with other entities as well. Since you are asking about a left-to-right ordering of bits versus a most-significant-to-least-significant ordering, you are asking about endianness. – Eric Postpischil Aug 22 '13 at 13:53
  • @FabioA.Correa: well, please erase your first comment, it's really confusing. Second, you may view a `char` value of one as 10000000, or 00000001... see Wikipedia as well: http://en.wikipedia.org/wiki/Most_significant_bit, the msb is sometimes called the left bit. – Dervin Thunk Aug 22 '13 at 13:56
  • NO! Endianness is not the same! Endiannes is about bytes, not bits!! – Dervin Thunk Aug 22 '13 at 13:57
  • Do you have a context in which something is referred to the "left bit" and that you need to know whether it is MSB or LSB? Also given the way that the `<<` and `>>` (left and right bitshift operators) are defined, I would be willing to bet that all systems treat the "left" bit as MSB, with respect to C – Dan F Aug 22 '13 at 13:59
  • @DanF: the context is here: en.wikipedia.org/wiki/Most_significant_bit, the msb is sometimes called the left bit. – Dervin Thunk Aug 22 '13 at 13:59
  • Ok and what does that have to do with programming? The `|` is sometimes called the pipe operator, but that doesn't change anything about the way it works – Dan F Aug 22 '13 at 14:00
  • @DanF: C 2011 (N1570) 6.5.7 4 and 5 effectively define “left” and “right” for bit-shifting operations. – Eric Postpischil Aug 22 '13 at 14:02
  • "Left" or "right" of bits can only be seen as the direction of the `<<` or `>>` operator. In that sense the sign bit will always be the most significant bit. – Jens Gustedt Aug 22 '13 at 14:02
  • @DervinThunk: Here is a non-byte endianness issue: The date 10/10/2012 is later than 11/11/2009, but it is earlier when sorted as a string. – Eric Postpischil Aug 22 '13 at 14:06
  • @EricPostpischil: Sure, English and Hebrew may be seen as a non-byte endianness issue, but endiannes was (and is) still used in computers mostly to refer to bytes in memory. In fact, we could see byte representations as bits are also a matter of endianness. – Dervin Thunk Aug 22 '13 at 14:08
  • @DervinThunk: “Mostly” does not equal “only”. – Eric Postpischil Aug 22 '13 at 14:09
  • @DervinThunk Besides endianess, Could you give us an example of a computer system in which the left bit of a byte is not the most significant bit? – Fabio A. Correa Aug 22 '13 at 14:11
  • oh, by the way: http://en.wikipedia.org/wiki/Endianness#.22Bit_endianness.22, this exists. Low order bits first. – Dervin Thunk Aug 22 '13 at 14:11
  • Maybe we are all asking the wrong questions. @DervinThunk What do you need this check for? – Fabio A. Correa Aug 22 '13 at 14:14
  • I would like to close the question, would you guys click on close? I have. The terminological mess is soo huge by all parties that it is meaningless until someone who does know stuff weighs in. – Dervin Thunk Aug 22 '13 at 14:17

2 Answers2

4

The only sense in which “left” and “right” are applied to bits is in left-shift and right-shift operations. In this sense, the most significant bit is always the leftmost bit; I have never seen a left-shift or right-shift defined otherwise.

The C standard defines the << operator both by saying that it left-shifts a value and that it multiplies the value by two to the power of the number of bits to shift (C 2011 (N1570) 6.5.7 4). It defines the >> operator similarly. By these definitions, left is toward more significant bits and right is toward less significant bits.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • True. There is one other context in which bit order is meaningful and matters: when the bits are being sent over a serial connection. But I don't see how that maps onto "left" and "right" since most communications cables are duplex; if the request is travelling right-to-left, the response will travel left-to-right. Still, it is definitely an endianness issue. – rici Aug 22 '13 at 14:36
0

If it is true the original question is asking if you can programmatically determine byte order, or endiness, then there are some excellent discussion posts on that topic.

Regarding endianess - There is a good discussion here. One of the posts sites this article. Both (either) should help to get you started on answering whatever question it is you intended.

Community
  • 1
  • 1
ryyker
  • 22,849
  • 3
  • 43
  • 87