Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

bit

endianness

Let L and B be two machines. L order its bits from LSB (Least Significant Bit) to MSB (Most Significant Bit) while B order from MSB to LSB. Or, in other words, L uses Little Endian while B uses Big Endian bit - not to be confused with byte - ordering.

Problem 1 SOLVED:

We are writing the following code which we want to be portable:

#include <stdio.h>

int main()
{
    unsigned char a = 1;
    a <<= 1;

    printf("a = %d\n", (int) a);

    return 0;
}

on L, it will print 2, but what happens on B? Will it shift the 1 out and print 0?.

SOLUTION: The C99 definition at 6.5.7 says it that, at least on unsigned integer types, << and >> will multiply and divide by 2 respectively.

Problem 2:

We are writing the following code which we want to be portable:

READ program:

/* program READ */
#include <stdio.h>

int main()
{
    FILE* fp;
    unsigned char a;

    fp = fopen("data.dat", "rb");
    fread(&a, 1, 1, fp);
    fclose(fp);

    return 0;
}

and WRITE program:

/* program WRITE */
#include <stdio.h>

int main()
{
    FILE* fp;
    unsigned char a = 1;

    fp = fopen("data.dat", "wb");
    fwrite(&a, 1, 1, fp);
    fclose(fp);

    return 0;
}

what happens if we run WRITE on L, move the data file to B and run READ there? And if we run WRITE on B and then READ on L?

Sorry if this is a FAQ. I googled for hours without luck.

like image 590
pudiva Avatar asked Mar 30 '11 23:03

pudiva


People also ask

Does endianness affect Bitwise Operators?

Endianness only matters for layout of data in memory. As soon as data is loaded by the processor to be operated on, endianness is completely irrelevent. Shifts, bitwise operations, and so on perform as you would expect (data logically laid out as low-order bit to high) regardless of endianness.

What does endianness affect?

Endianness. The attribute of a system that indicates whether integers are represented with the most significant byte stored at the lowest address (big endian) or at the highest address (little endian). Each address stores one element of the memory array.

Does endianness change bit order?

Endianness applied to only byte order. Not for bit order. The bit order remains same.


2 Answers

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.

like image 153
tomlogic Avatar answered Sep 26 '22 10:09

tomlogic


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.

like image 34
Carl Norum Avatar answered Sep 26 '22 10:09

Carl Norum