-1>>4 and -1<<4

S

soorajk007

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
 
C

cane

(e-mail address removed) 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.
 
V

void * clvrmnky()

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]).
 
W

Walter Roberson

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

cane

As a clarification: this is what happen under Windows with many
compilers (gcc, MS, watcom...).
 
W

Walter Roberson

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

pete

void said:
(e-mail address removed) wrote:
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.
 
E

Eric

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
 
F

Flash Gordon

Eric said:
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.
 
R

Robert Latest

On Thu, 18 May 2006 09:20:35 +0100,
in Msg. said:
Plz is programmable logic zimmerframe

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

robert
 
C

CBFalconer

Flash said:
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:
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
 
A

Al Balmer

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

Joe Wright

CBFalconer said:
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. ?
 
K

Keith Thompson

Joe Wright said:
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.
 
C

CBFalconer

Joe said:
CBFalconer wrote:
.... snip ...


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

Walter Roberson

Joe Wright wrote:
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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,201
Latest member
KourtneyBe

Latest Threads

Top