473,396 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

-1>>4 and -1<<4

Hi friends,

Im not an expert in the low level activities of C and I need to know
how the following works-
what is the difference b/w the way -1<<4 and -1>>4 works??
Plz plz explain bit by bit as much as possible...
Thanx in advance..

Bye
Sooraj

May 1 '06 #1
16 1851
so********@gmail.com ha scritto:
Hi friends,

Im not an expert in the low level activities of C and I need to know
how the following works-
what is the difference b/w the way -1<<4 and -1>>4 works??
Plz plz explain bit by bit as much as possible...
Thanx in advance..


On negative numbers: << and >> are implemented via arithmetic shifts. In
the case -1 << 4 the arithmetic left shift is the same as a logical left
shift. But in this case: -1 >> 4 the arithmetic right shift preserves
the sign bit copying it where you would normally expect 0s entering from
the left.

As for the results keep in mind that negative numbers are represented in
two's complement (if x is a positive number, -x is obtained by negating
x and adding 1 to it).

Let's suppose byte values: -1 = ~1 + 1 = 11111111. Hence:

-1 << 4 = 11110000 = -16

-1 >> 4 = 11111111 = -1

Bye.
May 1 '06 #2
so********@gmail.com wrote:
Im not an expert in the low level activities of C and I need to know
how the following works-
what is the difference b/w the way -1<<4 and -1>>4 works??
Plz plz explain bit by bit as much as possible...
Thanx in advance..

The result for (x >> y) when x is negative is "implementation defined".
You will have to read your compiler docs to find out what happens.
Assumptions about the results would not be portable.

For left-shift zeros fill the vacated spaces and any sign-bit will be
pushed off the end.

The standard also defines what Y may be given the promoted N-bit
representation of X (basically, it needs to be in the interval [0,N]).
May 1 '06 #3
In article <11**********************@i40g2000cwc.googlegroups .com>,
<so********@gmail.com> wrote:
Im not an expert in the low level activities of C and I need to know
how the following works-
what is the difference b/w the way -1<<4 and -1>>4 works??
Plz plz explain bit by bit as much as possible...


The difference is:

- left-shifting of a signed quantity is not discussed in the C89
standard [in particular, once one has reached the minimum integer, then
if one shifts again sign of the result is not discussed]

- right-shifting of a signed quantity is defined in the C89 standard
as being implementation dependant [in particular, it is up to
the implementation as to what bit value is injected into the newly-
vacated top bit.]

Left and right shifting of *unsigned* quantities have exact semantics
defined for all cases, but you asked about shifting of -1 (not about
values in general) and -1 is never an unsigned quantity...
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
May 1 '06 #4
As a clarification: this is what happen under Windows with many
compilers (gcc, MS, watcom...).
May 1 '06 #5
In article <44***********************@reader1.news.tin.it>,
cane <xx*@xxx.com> wrote:
On negative numbers: << and >> are implemented via arithmetic shifts. In
the case -1 << 4 the arithmetic left shift is the same as a logical left
shift.
Oh? What happens if one is using seperate sign or one's complement?
What is the meaning of "logical left shift" for those?
But in this case: -1 >> 4 the arithmetic right shift preserves
the sign bit copying it where you would normally expect 0s entering from
the left.
It appears that you haven't been reading the standards.

As for the results keep in mind that negative numbers are represented in
two's complement (if x is a positive number, -x is obtained by negating
x and adding 1 to it).


You *definitely* haven't been reading the standards carefully.
There are *three* integer representations possible in C89,
and two's complement is just one of them.
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
May 1 '06 #6
void * clvrmnky() wrote:

so********@gmail.com wrote:
-1<<4

For left-shift zeros fill the vacated spaces and any sign-bit will be
pushed off the end.


No, (-1<<4) is undefined.

N869
6.5.7 Bitwise shift operators
[#4] The result of E1 << E2 is E1 left-shifted E2 bit
positions; vacated bits are filled with zeros. If E1 has an
unsigned type, the value of the result is E1×2E2, reduced
modulo one more than the maximum value representable in the
result type. If E1 has a signed type and nonnegative value,
and E1×2E2 is representable in the result type, then that is
the resulting value; otherwise, the behavior is undefined.

--
pete
May 1 '06 #7
so********@gmail.com wrote:
Hi friends,

Im not an expert in the low level activities of C and I need to know
how the following works-
what is the difference b/w the way -1<<4 and -1>>4 works??
Plz plz explain bit by bit as much as possible...
Thanx in advance..

Bye
Sooraj


I'm at a loss, what in the world does b/w and plz plz mean?
I assume its a shorthand of some kind.. dont know
Eric

May 18 '06 #8
Eric wrote:
so********@gmail.com wrote:
Hi friends,

Im not an expert in the low level activities of C and I need to know
how the following works-
what is the difference b/w the way -1<<4 and -1>>4 works??
Plz plz explain bit by bit as much as possible...
Thanx in advance..

Bye
Sooraj


I'm at a loss, what in the world does b/w and plz plz mean?
I assume its a shorthand of some kind.. dont know


It's an abbreviation for black & white (this one is true). Plz is
programmable logic zimmerframe, which is a crutch for really old
computers. Unfortunately this does not seem to help in decoding the OPs
message. So perhaps the OP will have to try actually reading a C text
book, but I would suggest that the OP learn the difference between left
and right first.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
May 18 '06 #9
On Thu, 18 May 2006 09:20:35 +0100,
Flash Gordon <sp**@flash-gordon.me.uk> wrote
in Msg. <gb************@news.flash-gordon.me.uk>
Plz is programmable logic zimmerframe


Actually it ain't. Spelt in all-caps it means Postleitzahl (zip code).

robert
May 18 '06 #10
Flash Gordon wrote:
Eric wrote:
so********@gmail.com wrote:

Im not an expert in the low level activities of C and I need to
know how the following works-
what is the difference b/w the way -1<<4 and -1>>4 works??
Plz plz explain bit by bit as much as possible...
Thanx in advance..


I'm at a loss, what in the world does b/w and plz plz mean?
I assume its a shorthand of some kind.. dont know


It's an abbreviation for black & white (this one is true). Plz is
programmable logic zimmerframe, which is a crutch for really old
computers. Unfortunately this does not seem to help in decoding
the OPs message. So perhaps the OP will have to try actually
reading a C text book, but I would suggest that the OP learn the
difference between left and right first.


Well, glad that is cleared up. Of course, assuming the OP is
talking about C (which he should be, considering where he posted)
the expression "-1 << 4" results in undefined behaviour, and using
it could legitimately cause India to be permanently disconnected
from usenet, or pigs to fly. The fact that the expression has been
obfuscated by the foolish removal of all blanks does not affect the
pigs.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
May 18 '06 #11
On Thu, 18 May 2006 00:41:56 -0700, Eric <no****@email.com> wrote:
so********@gmail.com wrote:
Hi friends,

Im not an expert in the low level activities of C and I need to know
how the following works-
what is the difference b/w the way -1<<4 and -1>>4 works??
Plz plz explain bit by bit as much as possible...
Thanx in advance..

Bye
Sooraj


I'm at a loss, what in the world does b/w and plz plz mean?
I assume its a shorthand of some kind.. dont know
Eric


B/w is black and white. Plz is the German equivalent of Zip.

--
Al Balmer
Sun City, AZ
May 18 '06 #12
CBFalconer wrote:
Flash Gordon wrote:
Eric wrote:
so********@gmail.com wrote:
Im not an expert in the low level activities of C and I need to
know how the following works-
what is the difference b/w the way -1<<4 and -1>>4 works??
Plz plz explain bit by bit as much as possible...
Thanx in advance..
I'm at a loss, what in the world does b/w and plz plz mean?
I assume its a shorthand of some kind.. dont know

It's an abbreviation for black & white (this one is true). Plz is
programmable logic zimmerframe, which is a crutch for really old
computers. Unfortunately this does not seem to help in decoding
the OPs message. So perhaps the OP will have to try actually
reading a C text book, but I would suggest that the OP learn the
difference between left and right first.


Well, glad that is cleared up. Of course, assuming the OP is
talking about C (which he should be, considering where he posted)
the expression "-1 << 4" results in undefined behaviour, and using
it could legitimately cause India to be permanently disconnected
from usenet, or pigs to fly. The fact that the expression has been
obfuscated by the foolish removal of all blanks does not affect the
pigs.

-1 << 4 is -16. ?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
May 24 '06 #13
Joe Wright <jo********@comcast.net> writes:
CBFalconer wrote:

[...]
Well, glad that is cleared up. Of course, assuming the OP is
talking about C (which he should be, considering where he posted)
the expression "-1 << 4" results in undefined behaviour, and using
it could legitimately cause India to be permanently disconnected
from usenet, or pigs to fly. The fact that the expression has been
obfuscated by the foolish removal of all blanks does not affect the
pigs.

-1 << 4 is -16. ?


No. Where did you get that idea?

As both CBFalconer and the standard clearly state, -1 << 4 invokes
undefined behavior.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 24 '06 #14
Joe Wright wrote:
CBFalconer wrote:
.... snip ...
Well, glad that is cleared up. Of course, assuming the OP is
talking about C (which he should be, considering where he posted)
the expression "-1 << 4" results in undefined behaviour, and using
it could legitimately cause India to be permanently disconnected
from usenet, or pigs to fly. The fact that the expression has been
obfuscated by the foolish removal of all blanks does not affect the
pigs.


-1 << 4 is -16. ?


Not necessarily. It is either undefined or implementation defined.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
May 24 '06 #15
Joe Wright wrote:

CBFalconer wrote:
the expression "-1 << 4" results in undefined behaviour,

-1 << 4 is -16. ?


negative int shifted left: undefined
negative int shifted right: implementation defined
That's the actual answer to OP's original question about the
difference between -1>>4 and -1<<4

--
pete
May 25 '06 #16
In article <44***********@mindspring.com>,
pete <pf*****@mindspring.com> wrote:
Joe Wright wrote:
CBFalconer wrote:
> the expression "-1 << 4" results in undefined behaviour,
-1 << 4 is -16. ?

negative int shifted left: undefined
negative int shifted right: implementation defined
That's the actual answer to OP's original question about the
difference between -1>>4 and -1<<4


That's pretty much what I answered noticably earlier in the threads.
In C89 negative int shift left is not discussed... which does make
it "undefined" behaviour not just in the general semantic sense
but also in the specific C89 sense of "undefined" (which takes care
to include in "undefined" anything that is not defined by the standard.)
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
May 25 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.