Thread: Confusing bit shifting result

  1. #1
    ¡Amo fútbol!
    Join Date
    Dec 2001

    Confusing bit shifting result

    #include <iostream>
    int main(void){
         unsigned int foo = 0xFFFFFFFF;
         //i'd think these would return the same result, but
         //they don't in visual studio 2010 (debug/release).  why?
         std::cout<< (foo << 32) << " " << (0xFFFFFFFF << 32) <<std::endl;
         return 0;
    This came up cause I was trying to clear bits in a loop and some cases required clearing the entire value. I was trying to do so in a branch-free manner.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    For one thing, 0xFFFFFFFF is signed. For another, trying to shift more than or as many bits as there are bits in the left operand (after integral promotion) results in undefined behaviour.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Even when cast to an unsigned int, I got the same result. I never knew shifting a uint by 32 bits was undefined. Always assumed it'd be filled with 0's.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    You need to crank up your warnings. gcc says:
    [Warning] left shift count >= width of type

  5. #5
    Registered User
    Join Date
    Jan 2012
    you haven't said anything about your loop.

    clear foo with xor:

    std::cout<< (foo^foo) << " " << (0xFFFFFFFF ^ 0xFFFFFFFF) <<std::endl;

  6. #6
    Join Date
    Oct 2007
    Inside my computer
    Quote Originally Posted by c coder View Post
    clear foo with xor:
    ...Or just write 0 to it.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confusing pointers, Prints wierd result
    By sanddune008 in forum C Programming
    Replies: 4
    Last Post: 06-29-2010, 03:35 AM
  2. bit shifting
    By zecamoraes in forum C Programming
    Replies: 8
    Last Post: 11-24-2003, 10:41 AM
  3. bit shifting
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-14-2002, 12:31 AM
  4. bit shifting
    By cppdude in forum C++ Programming
    Replies: 9
    Last Post: 03-23-2002, 05:19 PM