public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: bit shifting doesn't work as expected
@ 2005-08-01 12:33 Matthew Jones
  0 siblings, 0 replies; 3+ messages in thread
From: Matthew Jones @ 2005-08-01 12:33 UTC (permalink / raw)
  To: gcc-help; +Cc: Constantin, Greubel

> 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.

To elaborate a bit on the other response : this is usually called
"sign extended shift right". It preserves the sign of a signed
integer, which is stored in the top bit (0 = +ve, 1 = -ve).

If you have 2 signed integers, a, and b, such that a = -b, then shift
them both x bits right, then it will still be true that a = -b.

If the sign bit is not preserved, -ve numbers change into very large
+ve numbers after a shift right. For example, for 16 bit signed
integers:
 non sign extended: 11000000 (-64) >>2  ==>  00110000 (48)
 sign extended:     11000000 (-64) >>2  ==>  11110000 (-16)

The first one is correct if the numbers are UNSIGNED, and the first
represents +192 rather than -64.

> And the next question would be: How can I manage it to get zeros,
> instead of ones?

As suggested, use unsigned data types.

HTH

--
Matthew JONES
http://www.tandbergtv.com/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: bit shifting doesn't work as expected
  2005-07-31 21:27 Constantin Greubel
@ 2005-07-31 22:11 ` Ian Lance Taylor
  0 siblings, 0 replies; 3+ messages in thread
From: Ian Lance Taylor @ 2005-07-31 22:11 UTC (permalink / raw)
  To: Constantin Greubel; +Cc: gcc-help

Constantin Greubel <constantin.greubel@physik.uni-muenchen.de> writes:

> 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?

Your question would be easier to understand if you showed the output
of your program, and also showed the expected output.

However, I suspect that the answer to your question is that doing a
right shift of a variable with a signed type which happens to hold a
negative value is implementation defined.  If you want to reliably and
portably do a right shift of a value with the high bit set, use
'unsigned int' instead of 'int'.

Ian

^ permalink raw reply	[flat|nested] 3+ messages in thread

* bit shifting doesn't work as expected
@ 2005-07-31 21:27 Constantin Greubel
  2005-07-31 22:11 ` Ian Lance Taylor
  0 siblings, 1 reply; 3+ messages in thread
From: Constantin Greubel @ 2005-07-31 21:27 UTC (permalink / raw)
  To: gcc-help

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;
}

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-08-01 12:33 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-08-01 12:33 bit shifting doesn't work as expected Matthew Jones
  -- strict thread matches above, loose matches on Subject: below --
2005-07-31 21:27 Constantin Greubel
2005-07-31 22:11 ` Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).