Confusing bit shifting result

This is a discussion on Confusing bit shifting result within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> int main(void){ unsigned int foo = 0xFFFFFFFF; //i'd think these would return the same result, but //they ...

  1. #1
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136

    Confusing bit shifting result

    Code:
    #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
    Location
    Singapore
    Posts
    22,303
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    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
    Posts
    2,808
    You need to crank up your warnings. gcc says:
    [Warning] left shift count >= width of type

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

    clear foo with xor:

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

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    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, 04: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, 01:31 AM
  4. bit shifting
    By cppdude in forum C++ Programming
    Replies: 9
    Last Post: 03-23-2002, 05:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21