Topic: Order of bits in physical memory


Author: Eric Wolf <eric@boese-wolf.eu>
Date: Thu, 20 May 2010 12:08:35 CST
Raw View
Is there any guaranty by the c++ standard
for the order of the bits in memory?

If I, for example, want to send a byte to the
serial port of the computer to control some electronic
device, the order of the bits in the bytes is significant.

I searched for something like that in
http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2010/n3090.pdf
but hadn't been able to recognise something.

As a byte might have different numbers of bits, its all (also the
ordering of bits in a byte) implementation defined, I assume?

Yours sincerely,

Eric

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Balog Pal" <pasa@lib.hu>
Date: Thu, 20 May 2010 16:58:40 CST
Raw View
"Eric Wolf" <eric@boese-wolf.eu>

> Is there any guaranty by the c++ standard
> for the order of the bits in memory?
>
> If I, for example, want to send a byte to the
> serial port of the computer to control some electronic
> device, the order of the bits in the bytes is significant.
>

This hardly makes sense. The memory will not send your bits in any way. For
seraial port you will have some UART, and it will specify with an 'data to
send' register, and it will specify how it will send the bits (possibly make
it configurable).  What will count is oonly he value of the byte.

If you shst bits away by hand, then you can do it your way do it by >> or
<<.

I searched for something like that in
> http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2010/n3090.pdf
> but hadn't been able to recognise something.
>

The more practical question could be whether C++ defines endianness. (I.e.
how bits are distributed in multi-byte objects.

say we have

int i = 0x01020304;
char c[ sizeof (i) ];
memcpy(c, &i, sizeof (i));

then what we have in c[0], c[1], ...

It is not specified by the standard, and implementations are actually using
more than two different orderings. Normally it depends on the
processor/architecture, and compilers for tha target follow the hardware.




--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: David Krauss <potswa@gmail.com>
Date: Sat, 22 May 2010 02:24:49 CST
Raw View
On May 20, 1:08 pm, Eric Wolf <e...@boese-wolf.eu> wrote:
> Is there any guaranty by the c++ standard
> for the order of the bits in memory?
>
> If I, for example, want to send a byte to the
> serial port of the computer to control some electronic
> device, the order of the bits in the bytes is significant.

I think Balog is right (didn't read it all), but put simply, that is
defined by the serial protocol. RS-232 transmits the LSB first.

Conventionally "left" is towards the MSB but there's no real reason we
can't reverse the names of left and right. The bits have no order
besides their different place values.


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Eric Wolf <eric@boese-wolf.eu>
Date: Sun, 23 May 2010 20:33:04 CST
Raw View
"Balog Pal" <pasa@lib.hu> writes:

> "Eric Wolf" <eric@boese-wolf.eu>
>
>> Is there any guaranty by the c++ standard
>> for the order of the bits in memory?
>>
>> If I, for example, want to send a byte to the
>> serial port of the computer to control some electronic
>> device, the order of the bits in the bytes is significant.
>>
>
> This hardly makes sense. The memory will not send your bits in any
> way.

As you noticed, I don't have a clue about serial protocols. I thought it
might be an example.

Can one summarize that there is no situation, where the order of
the bits matter?

> The more practical question could be whether C++ defines endianness. (I.e.
> how bits are distributed in multi-byte objects.
>
> say we have
>
> int i = 0x01020304;
> char c[ sizeof (i) ];
> memcpy(c, &i, sizeof (i));
>
> then what we have in c[0], c[1], ...
>
> It is not specified by the standard, and implementations are actually using
> more than two different orderings. Normally it depends on the
> processor/architecture, and compilers for tha target follow the hardware.

But is it mandatory by the C++ standard for the the manufacturer of the
compiler to document that?

mfg

Eric

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: John Nagle <nagle@animats.com>
Date: Wed, 26 May 2010 11:52:35 CST
Raw View
Eric Wolf wrote:

> "Balog Pal" <pasa@lib.hu> writes:
>
> "Eric Wolf" <eric@boese-wolf.eu>
>>
>
The more practical question could be whether C++ defines endianness. (I.e.
>> how bits are distributed in multi-byte objects.
>>
>> say we have
>>
>> int i = 0x01020304;
>> char c[ sizeof (i) ];
>> memcpy(c, &i, sizeof (i));
>>
>> then what we have in c[0], c[1], ...
>>
>> It is not specified by the standard, and implementations are actually
>> using
>> more than two different orderings. Normally it depends on the
>> processor/architecture, and compilers for the target follow the hardware.
>>
>
> But is it mandatory by the C++ standard for the the manufacturer of the
> compiler to document that?
>

   It's not specified by the standard.  There are "big endian" and
"little endian" machines, with different byte orders.  x86, etc. are
little endian.  Motorola 68K etc. are "big endian".  Later SPARC,
ARM, PPC, and Itainum machines can be run in either mode, but one
usually picks a mode at boot time and never changes it.

   Bit order within numbers is, however, relatively standard today.
A right shift (">>") of an unsigned value by one bit is a divide by two.

                                       John Nagle

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: CornedBee <wasti.redl@gmx.net>
Date: Wed, 26 May 2010 11:53:11 CST
Raw View
On May 24, 4:33 am, Eric Wolf <e...@boese-wolf.eu> wrote:
>
> But is it mandatory by the C++ standard for the the manufacturer of the
> compiler to document that?

Apparently not. At least, I couldn't find anything.

Sebastian


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: James Kanze <james.kanze@gmail.com>
Date: Sat, 29 May 2010 15:29:08 CST
Raw View
On May 22, 9:24 am, David Krauss <pot...@gmail.com> wrote:
> On May 20, 1:08 pm, Eric Wolf <e...@boese-wolf.eu> wrote:

> > Is there any guaranty by the c++ standard
> > for the order of the bits in memory?

> > If I, for example, want to send a byte to the
> > serial port of the computer to control some electronic
> > device, the order of the bits in the bytes is significant.

> I think Balog is right (didn't read it all), but put simply, that is
> defined by the serial protocol. RS-232 transmits the LSB first.

> Conventionally "left" is towards the MSB but there's no real reason we
> can't reverse the names of left and right. The bits have no order
> besides their different place values.

There is no order in memory, but there is an order in the way we
name the bits: on most systems, bit 0 is the low order, right
most bit, but I've seen systems where there was no bit 0, and
bit 1 was the high order, left most bit.  (In the documentation.
On such systems, bit 32 had the value of 2^0, bit 31 2^1, etc.)

--
James Kanze

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: franl <flitterio@gmail.com>
Date: Tue, 1 Jun 2010 13:34:26 CST
Raw View
On May 26, 1:52 pm, John Nagle wrote:

> There are "big endian" and
> "little endian" machines, with different byte orders.

And let's not forget middle-endian (aka PDP-endian):

http://en.wikipedia.org/wiki/Endianness#Middle-endian


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: ThosRTanner <ttanner2@bloomberg.net>
Date: Fri, 4 Jun 2010 17:44:50 CST
Raw View
On May 26, 6:52 pm, John Nagle <na...@animats.com> wrote:

>    Bit order within numbers is, however, relatively standard today.
> A right shift (">>") of an unsigned value by one bit is a divide by two.

Right shift may be unambiguous, but whether the LSB or MSB is numbered
0 is definitely not.

And you are completely on your own if you are using bitfields in
structures. I've seen that change between 2 different versions of the
same compiler.


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]