bit shifting doesn't work as expected

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

I hope this is at least the correct address to ask such questions.
I have the following small programm, and I don't understand why the number is feeded with ones from the right, but not with zeros. And the next question would be: How can I manage it to get zeros, instead of ones?

I'm still using gcc-3.4.4 but with a bunch of Gentoo patches. I tried the 4.0.1 cvs, but it gave me several errors.
Thank you

void printint(unsigned long int toprint){
       int i, var=toprint;
       printf("printint: %d\n", var);
       for(i=0; i<32; i++){
               printf("%d_", var & 0x1);
               var >>= 1;
       }
       printf("\n");
}

int main(){
       int i, end, srt = 1;
//This gives us the number -2147483648, which has only the first bit a one, and the rest zeros
       for(i=0; i<31; i++){
               srt <<= 1;
       }
       printf("the number reads: %d\n", srt);
       end = 0;
       for(i=0; i<32; i++){
               printint(end);
               end >>= 1;
               if(i==3){//not necessary 3, but any number x: 0<x<32
                       printf("inif!!!!!!!!!!!!!!!!!!!!!!1\n");
                       end |= srt;//-2147483648
               }
       }
       printf("FINAL: %d\n", end);
       return 0;
}

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux