Little Endian Shifts

NOTE: This is the first post on my blog and just exists to become acquainted with wordpress. Nothing serious will be presented in this post.

It is a common programming exercise for CS students to write a function that determines whether a machine has big- or little-endian byte order.

There are a bunch of standard solutions to this problem like using unions…

int is_little_endian (void) {
    union {
        long value;
        unsigned char bytes [sizeof(long)];
    } data;

    data.value = 1;

    return data.bytes[0];
}

… or using char pointers …

int is_little_endian (void) {
    long value = 1;

    return *( (char*)&value );
}

However when I first tried to solve this I had something different in mind.
My goal was to make the endianness check as minimal as possible when I stumbled
upon a very misleading naming convention.
My idea was to use something like this:

int is_little_endian (void) {
    long value = 1;

    return value >> 1; /* Obviously wrong but bare with me... */
}

Being too deep into endianness at that moment I thought:
On little-endian systems value has a binary representation like 01 00 00 00
so if value gets shifted to the right value becomes 00 80 00 00
and if this code gets executed on a big-endian machine value is 00 00 00 01
becoming 00 00 00 00 when shifted to the right.

It turned out that the instructions SHL and SHR are not moving the bitstring itself but the big-endian representation of the value in the given register. In fact all binary operations respect the big-endian byte order and there is not a single instruction operating on a bitstring in a register as is.
The Intel® 64 and IA-32 Architectures Software Developer’s Manual is a bit more precise about this:

The shift arithmetic right (SAR) and shift logical right (SHR) instructions shift the bits of the destination operand to the right (toward less significant bit locations).

(Source)

The shift arithmetic left (SAL) and shift logical left (SHL) instructions […] shift the bits in the destination operand to the left (toward more significant bit locations).

(Source)

So it is not about left or right it is about the more significant or the less significant end of a bitstring. This also affects other instructions that contain the word left or right like ROL/ROR.

So the lesson here is, I guess: Don’t think too low-level.

Design a site like this with WordPress.com
Get started