Thread: Bitshift with different endianess

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Bitshift with different endianess

    Hello!

    Consider this code:

    Code:
    unsigned int i = 1;
    i <<= 8;
    printf("%d\n", i);
    If the processor operates with little endianess, the code will print 256. But what if it has big endianess, will it print 0?
    Come on, you can do it! b( ~_')

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    1 << 8 will be 256 in all systems.

    The byte-order (endianness) is only relevant when the processor stores the data in memory. The internal structure of a CPU register has all bits in a logical order from 0..n, in a logical right to left order. This register may be stored as 4 bytes in either big or little endian order - but shifting it 8 bits will still shuffle the entire number 8 bits to the left (or right), because this happens internally in the CPU, not on the memory outside the CPU.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Ok, but this example:

    Code:
    char a[4];
    a[0] = 0;
    a[1] = 1;
    a[2] = 2;
    a[3] = 3;
    printf("&#37;.8X", *(int*)a);
    will print 00010203 if the processor uses little endianess, but 03020100 if the processor uses big endianess? Or have I got it wrong?
    Come on, you can do it! b( ~_')

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes (unless I mixed up which endianess is what).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Quote Originally Posted by TriKri
    ... will print 00010203 if the processor uses little endianess, but 03020100 if the processor uses big endianess? Or have I got it wrong?
    In fact, it's the inverse, if the processor is little endian, it will print 03020100, and if it's big endian, it will print 00010203.

    Of course, we are also assuming that the processor is adressing bytes, and not something more "exotic", e.g. 16-bits words.
    I hate real numbers.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    in other words, as long as you don't reinterpret the bit pattern as some other type then whatever manipulations/transformations you perform will be portable. otherwise, you are treading the waters of undefined behavior.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. endianess problem!!!!
    By sandeepvignesh in forum Networking/Device Communication
    Replies: 2
    Last Post: 06-17-2009, 04:58 AM
  2. undo bitshift
    By young turk in forum C Programming
    Replies: 6
    Last Post: 04-28-2009, 10:08 AM
  3. Bitshift Crypt
    By ratte in forum C++ Programming
    Replies: 11
    Last Post: 01-10-2008, 02:48 PM
  4. Bitshift on 64-bit integers.
    By maththeorylvr in forum C Programming
    Replies: 16
    Last Post: 03-18-2005, 04:19 PM
  5. stream / bitshift overloads
    By Trauts in forum C++ Programming
    Replies: 1
    Last Post: 05-17-2003, 06:58 PM