11

I have to implement a protocol which defines data in 8bit words, which starts with the least significant bit (LSB) first. I want to realize this data with unsigned char, but I don't know what's the bit order of LSB and most significant bit (MSB) in C/C++, that could possible require swapping the bits.

Can anybody explain me how to find out an unsigned char is encoded: with MSB-LSB or LSB-MSB?

Example:

unsigned char b = 1;

MSB-LSB: 0000 0001 LSB-MSB: 1000 0000

Mahmoud Al-Qudsi
  • 28,357
  • 12
  • 85
  • 125
Dudero
  • 607
  • 7
  • 15
  • possible duplicate of [Detecting endianness programmatically in a C++ program](http://stackoverflow.com/questions/1001307/detecting-endianness-programmatically-in-a-c-program) – Armen Tsirunyan Sep 29 '11 at 12:27
  • 6
    are you sure you want bit ordering and not byte ordering? normally when talking about protocols LSB means least significant byte. Same for MSB. – sashang Sep 29 '11 at 12:28
  • In particular, the second answer to that question is probably what you are looking for. – Matthew Scharley Sep 29 '11 at 12:30
  • 1
    @ArmenTsirunyan - this question is just a shade different to that question, insofar as the other question asks about the endian-ness you'd expect to matter, and this one asks about endian-ness you'd usually never worry about. In particular, I don't see how answers to that question would satisfy this one. – JustJeff Sep 29 '11 at 13:03

2 Answers2

13

Endian-ness is platform dependent. Anyway, you don't have to worry about actual bit order unless you are serializing the bytes, which you may be trying to do. In which case, you still don't need to worry about how individual bytes are stored while they're on the machine, since you will have to dig the bits out individually anyway. Fortunately, if you bitwise AND with 1, you get the LSB, regardless of storage order; bit-AND with 2 and you get the next most significant bit, and so on. The compiler will sort out what constants to generate in the machine code, so that level of detail is abstracted away.

JustJeff
  • 12,640
  • 5
  • 49
  • 63
  • 2
    Network and file stuff rarely gets to bit level either, unless you're writing a device driver. All useful user-level stuff works in terms of bytes. – cHao Sep 29 '11 at 12:30
5

There is no such thing in C/C++. The least significant bit is -- well -- the least significant bit. Since the bits don't have addresses, there is no other ordering.

undur_gongor
  • 15,657
  • 5
  • 63
  • 75
  • 1
    During transmission, there is. – user703016 Sep 29 '11 at 12:31
  • @Cicada: But not in C? I mean you can split the byte in one order or the other but there is no platform-dependent correct order. – undur_gongor Sep 29 '11 at 12:33
  • 4
    Indeed there is no such thing as bit order in C. However different physical architectures may store bits in different orders. Since OP is developing a protocol, I believe it is important to explain the difference. – user703016 Sep 29 '11 at 12:35
  • 1
    @Cicada: unless you implement hardware and actually wiring some chips together - there is no "bit order" stuff. You can screw up this things only if some chip takes bits from serial line and put it in register in incorrect order. When data is in main memory and CPU-accessible - bit order is correct and can't be broken. The only reason we have all this endianess problems with words is because we have data types smaller than word. Fortunately, there is no such trouble with bytes. – blaze Sep 29 '11 at 14:13
  • @blaze: At the risk of repeating myself, I am pinpointing the problem of bit order during *transmission*, NOT storage. – user703016 Sep 29 '11 at 14:22
  • @Cicada But what takes place during transmission isn't accessible from a program. – James Kanze Sep 29 '11 at 16:44
  • @JamesKanze: oh yes it is, if your program isn't being bound by an OS it may be possible. – user703016 Sep 29 '11 at 16:58
  • @Cicada Not in C++ itself. In environments where there isn't an OS, it's frequent for the implementation to provide extensions, and some of these **might** provide this (although I've never seen it), but in C++ itself, the smallest addressable unit is the byte. – James Kanze Sep 30 '11 at 07:27
  • @JamesKanze: I'm afraid this is pretty much what I said. No bit order in levels higher than hardware. – user703016 Sep 30 '11 at 10:13