How bit endianness affects bitwise shifts and file IO in C?

Bit Endianness doesn't affect data stored on disks in bytes. Byte Endianness will.

Bit Endianness doesn't affect data stored on disks in bytes. Byte Endianness will. Bit Endianness is something that matters for serial interfaces where a byte is sent one bit at a time, and the sender and receiver need to agree on the byte order.

For example, bit order in SPI devices varies and you need to reference the data sheet before attempting to read from the device. Here's what Wikipedia says on bit endianness: The terms bit endianness or bit-level endianness are seldom used when talking about the representation of a stored value, as they are only meaningful for the rare computer architectures where each individual bit has a unique address. They are used however to refer to the transmission order of bits over a serial medium.

Most often that order is transparently managed by the hardware and is the bit-level analogue of little-endian (low-bit first), although protocols exist which require the opposite ordering (e.g. I²C). In networking, the decision about the order of transmission of bits is made in the very bottom of the data link layer of the OSI model.In your case, the physical hard drive interface defines the bit order, regardless of the processor that's going to read or write it.

So you say Endianness of bits is irrelevant at file IO. Is this some kind of standard? If so, can you post links to where it is defined?

– rogi Mar 30 '11 at 23:35 Now this makes some sense. – rogi Mar 30 '11 at 23:39 File I/O is defined as reading complete bytes. You can't read less than a byte from a file.

The bit order depends on the interface to the storage medium (SCSI, SATA, USB Mass Storage, etc. ) and I would imagine that only serial interfaces (e.g. , SATA butnot PATA) are affected. – tomlogic Mar 30 '11 at 23:39 Hmm, no, unlike cpu cores these interfaces are actually described in fine detail in a standard so that devices from different manufacturers will work together. – Hans Passant Mar 30 '11 at 23:42 Hm... After all, bit endianness is relevant or not when reading and writing?

– rogi Mar 30 '11 at 23:57.

1 Technically, they don't. There's a difference between /2 and >>1 when using negative numbers. Also, since some CPUs are much slower to divide than to shift, they will certainly internally do a shift operation.

– EboMike Mar 30 '11 at 23:23 In fact you are almost right. C99 standard define >> and > 1 = -5. Division and shift are NOT the same.

– EboMike Mar 31 '11 at 0:01.

Bit-shifting is not affected by endianness. Binary file I/O normally is, but not in your case since you are only writing a single byte.

1 I'm concerned about bit endianness and not byte. – rogi Mar 30 '11 at 23:09 1 What platform has a different "bit" endianness?! – EboMike Mar 30 '11 at 23:12 1 In any case, anything not giving you "2" as the result of "1 But that's byte order, not bit order.

– EboMike Mar 30 '11 at 23:18.

There isn't really such a thing as bit-endianness, at least as far as C is concerned. CHAR_BIT has to be at least 8 according to the spec, so accesses to any objects smaller than that is pretty much meaningless to a standard C program. Regardless of how the hardware stores a byte - LSB or MSB first - it doesn't affect your program at all.

MyVar & 1 returns the right bit in either case. If you need to interact with some kind of serial interface and reconstitute bytes from it, that's a different story. Your own machine's 'bit-endianness' still doesn't affect anything, but the bit order of the interface certainly does.

Now, as to your specific question and the program you've shown. Your programs are almost 100% portable. Neither bit- nor byte-endianness affects them.

What might affect them is if CHAR_BIT were different on each platform. One computer might write more data than the other one would read, or vice versa.

Your answer made me think: if character size is implementation-defined, there is just no way to make any portable file with fread and fwrite. Am I right? – rogi Mar 31 '11 at 0:11 1 @rogi, sure it can be portable - just make sure you're copying the right number of bits!

You can use the CHAR_BIT constant to keep your programs portable. – Carl Norum Mar 31 '11 at 0:35 Oh. Thanks for your answer!

I was gonna ask another question about this issue! – rogi Mar 31 '11 at 0:41.

Endianness will not affect you unless a) you read something from memory using a different type than you used to write it to, or if you read something from a file that was written with a machine using different endianness. I.e. Int data; char charVal; *data = 1; charval = *((char *) data); // Different result based on endianness Or your example two, assuming you're using a type larger than char.

I want to know about bits and not bytes. Your example is an undefined behaviour. – rogi Mar 30 '11 at 23:19 3 See my comments to the other answer.

You might have misunderstood something. Big-endian and little-endian refers to the byte order. – EboMike Mar 30 '11 at 23:20 I refer to bit order.

Teh question emphasizes that. – rogi Mar 30 '11 at 23:22 1 Yes, and both Hans and myself have been trying to tell you that what you're talking about doesn't exist in real life. – EboMike Mar 30 '11 at 23:25 2 @tomlogic: It does, I should clarify.

Bit Endianness doesn't affect you when your smallest atomic read operation is a byte (the article itself mentions how bit endianness this is relevant). When programming in C and dealing with data stored in memory, only byte order matters. – EboMike Mar 30 '11 at 23:36.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions