Bit Shiftting

This is a discussion on Bit Shiftting within the C Programming forums, part of the General Programming Boards category; Hello All, I'm trying to shift bit0 as follows: unsigned long int tim; tim = 1; tim = tim << ...

  1. #1
    BitTest
    Guest

    Question Bit Shiftting

    Hello All,

    I'm trying to shift bit0 as follows:

    unsigned long int tim;

    tim = 1;

    tim = tim << 31;

    printf("tim = %ld",tim);

    output: stc = -2147483648

    However, I'm expecting to be -> 0x80000000 (+2147483648)

    Why i get the negative number? By the way I'm building 32-bit console application under borland C++ and size of long is 4 bytes.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Because %ld is not an unsigned format.

  3. #3
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Code:
    #include <stdio.h>
    int main()
    {
    	unsigned int tim = 1;
    
    	tim <<= 31;
    
    	printf("tim = %u\n",tim);
    
    	return 0;
    }
    Here
    The keyboard is the standard device used to cause computer errors!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-10-2005, 09:53 AM
  2. porting application from 32 bit to 64 bit error
    By gandalf_bar in forum Linux Programming
    Replies: 1
    Last Post: 09-14-2005, 09:20 AM
  3. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  4. Bit Manipulation Questions
    By CPPNewbie in forum C++ Programming
    Replies: 7
    Last Post: 08-12-2003, 02:17 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM

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