Results 1 to 5 of 5

Thread: Verifying Endianness

  1. #1
    Join Date
    Mar 2011
    Beans
    144
    Distro
    Ubuntu 12.04 Precise Pangolin

    Verifying Endianness

    I believe are linux commands that can output the value of endianness.

    Would it also be true to assume that a bitwise shift left will indicate the value of endianness? Where given a integral value value, v, and a number of left shifts, n, if (v * n^2) > v this implies Big Endian and vice versa?

  2. #2
    Join Date
    Mar 2010
    Location
    Dhaka, Bangladesh
    Beans
    210
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Verifying Endianness

    I don't know about commands, but this code should determine the endianness
    Code:
    #define BIG_ENDIAN 0
    #define LITTLE_ENDIAN 1
    
    int TestByteOrder() {
        short int word = 0x0001;
        char *byte = (char *) &word;
        return (byte[0] ? LITTLE_ENDIAN : BIG_ENDIAN);
    }

  3. #3
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Verifying Endianness

    Quote Originally Posted by 3246251196 View Post
    Would it also be true to assume that a bitwise shift left will indicate the value of endianness? Where given a integral value value, v, and a number of left shifts, n, if (v * n^2) > v this implies Big Endian and vice versa?
    If you mean in C, it's a big no! All operators in C work on values, not bits. (1 << 5) will always be 32, regardless of endianness. One way to test for it is:

    Code:
    unsigned int n = 1;
    bool islittle = *((unsigned char*)(&n)) == 1;
    「明後日の夕方には帰ってるからね。」


  4. #4
    Join Date
    Mar 2011
    Beans
    144
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Verifying Endianness

    Thank you for your two replies. Identical.

    We are saying:
    1- Here is a 4 byte integer
    2- Let the first byte (lowest address) of the memory that stores integer be converted to a character (so as to represent 1 byte)
    3- If this byte, when de-referenced, contains the value 1, then we are working with little, else big.

  5. #5
    Join Date
    Mar 2010
    Location
    Dhaka, Bangladesh
    Beans
    210
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Verifying Endianness

    Quote Originally Posted by 3246251196 View Post
    Thank you for your two replies. Identical.

    We are saying:
    1- Here is a 4 byte integer
    2- Let the first byte (lowest address) of the memory that stores integer be converted to a character (so as to represent 1 byte)
    3- If this byte, when de-referenced, contains the value 1, then we are working with little, else big.
    Yes, and Bachstelze explained why bit shifting won't work, even if they seem correct theoretically.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •