Bitwise Shift

This is a discussion on Bitwise Shift within the C Programming forums, part of the General Programming Boards category; When I print this out, is it suppose to be two identical columns? Also the last number I see for ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    58

    Bitwise Shift

    When I print this out, is it suppose to be two identical columns? Also the last number I see for both columns is -2147483648, is there something wrong with that?

    Code:
    #include <stdio.h>
    
    int main( int argc, char *argv[] ){
    
      int x = 1;
      unsigned int y = 1;
      int i;
    
      int limit = sizeof(int) * 8;
    
      printf("The value for x is %d. \n", x);
      printf("The value for y is %d. \n\n", y);
    
      printf("Signed \t\t     Unsigned \n");
    
      for(i = 0; i < limit - 1; i++){
        
        x = x << 1;
        y = y << 1;
        
        printf("%d %20d \n", x, y); 
      }
      
      return 0;
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by blurx View Post
    When I print this out, is it suppose to be two identical columns?

    Code:
     
        printf("&#37;d %20d \n", x, y);
    Yes.



    [edit: nonoob has it right -- change %20d to %20u]
    Last edited by MK27; 10-12-2008 at 09:45 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Your output is fine. Shifting left is unaffected by signed or unsignedness. Shifting right may not be.
    You've specified that printf() interprets the numbers as signed in both cases. So it does.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bitwise shift operators in -ve numbers
    By rohit_second in forum C Programming
    Replies: 11
    Last Post: 09-15-2008, 01:18 PM
  2. still problems with ceasar shift
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 09-14-2008, 01:49 AM
  3. Ceasar Shift program
    By trevordunstan in forum C Programming
    Replies: 11
    Last Post: 09-11-2008, 09:40 PM
  4. Bitwise Shift Operations
    By John_L in forum Tech Board
    Replies: 19
    Last Post: 02-25-2008, 10:22 AM
  5. Simulating Shift Key
    By PrimeTime00 in forum Windows Programming
    Replies: 7
    Last Post: 10-15-2004, 05:53 AM

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