public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Matthew Jones <matthewjones@vw1600e.org.uk>
To: gcc-help@gcc.gnu.org
Cc: Constantin@vw1600e.org.uk, Greubel@vw1600e.org.uk
Subject: RE: bit shifting doesn't work as expected
Date: Mon, 01 Aug 2005 12:33:00 -0000	[thread overview]
Message-ID: <1122899611.42ee169b74f47@webmail.vw1600e.org.uk> (raw)

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

             reply	other threads:[~2005-08-01 12:33 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-08-01 12:33 Matthew Jones [this message]
  -- strict thread matches above, loose matches on Subject: below --
2005-07-31 21:27 Constantin Greubel
2005-07-31 22:11 ` Ian Lance Taylor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1122899611.42ee169b74f47@webmail.vw1600e.org.uk \
    --to=matthewjones@vw1600e.org.uk \
    --cc=Constantin@vw1600e.org.uk \
    --cc=Greubel@vw1600e.org.uk \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).