Prashant Neginahal

Ranch Hand

Posts: 76

Barry Gaunt

Ranch Hand

Posts: 7729

posted 14 years ago

-1 is an int, so take the low 5 bits of the 32 = 100000B.

Gives you 00000B which is 0.

Logical shift -1 to right by 0 bits.

Answer -1.

-Barry

Gives you 00000B which is 0.

Logical shift -1 to right by 0 bits.

Answer -1.

-Barry

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch

Getting someone to think and try something out is much more useful than just telling them the answer.

John Lee

Ranch Hand

Posts: 2545

posted 14 years ago

In binary, 1 = 00000001 (assume 8-digit, same below)

The definetion of -1 is 11111111, here the most signaficant digit 1 means "-", the rest swap from "00000001", then plus 1 in the least signaifcant digit.

So 1 + (-1) = 00000001 + 11111111 = 00000000 = 0

It makes sense.

so, -1 >>> x (any number) is still -1, according the defination of ">>>".

Originally posted by Prashant Neginahal:

Hello Everybody,

How -1>>>32 is equal to -1.?Pls explain.

------------

Prashant

In binary, 1 = 00000001 (assume 8-digit, same below)

The definetion of -1 is 11111111, here the most signaficant digit 1 means "-", the rest swap from "00000001", then plus 1 in the least signaifcant digit.

So 1 + (-1) = 00000001 + 11111111 = 00000000 = 0

It makes sense.

so, -1 >>> x (any number) is still -1, according the defination of ">>>".

Prashant Neginahal

Ranch Hand

Posts: 76

posted 14 years ago

Firstly, do you accept that -1(decimal) = 11111111(2's complement representation)?

Secondly, the '>>>' operation pushes everything to the right and fills with 1's

Therefor 11111111 >>> 1 = 1111111 with a '1' placed at the start.

Hmm, lets talk about binary positions one to eight

Position 1 moves to position two, two to three, etc up to position eight, which just 'plopps off'.

12345678 >>> 1 -> X1234567

Right shift one place, all bits move one to the right and the last falls off. This leave an unfilled position 'X'. With the '>>>' operator, this position gets filled with a '1'

Now -1 = 11111111

11111111 >>> 1 = 11111111 = -1

11111111 >>> 2 = 11111111 = -1

etc etc

11111111 >>> (any number) = -1

Dave

Secondly, the '>>>' operation pushes everything to the right and fills with 1's

Therefor 11111111 >>> 1 = 1111111 with a '1' placed at the start.

Hmm, lets talk about binary positions one to eight

Position 1 moves to position two, two to three, etc up to position eight, which just 'plopps off'.

12345678 >>> 1 -> X1234567

Right shift one place, all bits move one to the right and the last falls off. This leave an unfilled position 'X'. With the '>>>' operator, this position gets filled with a '1'

Now -1 = 11111111

11111111 >>> 1 = 11111111 = -1

11111111 >>> 2 = 11111111 = -1

etc etc

11111111 >>> (any number) = -1

Dave

John Lee

Ranch Hand

Posts: 2545

posted 14 years ago

To my memory, the definetion of bit shift operation is:

1) for positive numbers, >>x equals to losing last x bits and add x '0...0' to the left;

2) for positive numbers, <<x equals to losing first x bits and add x '0...0' to the right;

3) for negative numbers, <<x equals to losing first x bits and add x '0...0' to the right, but make sure the most signaficant bit is '1' afterwards;

4) for negative numbers, >>x equals to losing last x bits and add x '1...1' to the left;

So no matter how you apply >> to -1 (1...1), it is still -1 (1...1).

[ December 17, 2002: Message edited by: Don Liu ]

Originally posted by Prashant Neginahal:

hey Don!!!

ur statement

-1 >>> x (any number) is still -1, according the defination of ">>>"..

How can it be man!!!?

Pls justify ur answer.

Prashant

To my memory, the definetion of bit shift operation is:

1) for positive numbers, >>x equals to losing last x bits and add x '0...0' to the left;

2) for positive numbers, <<x equals to losing first x bits and add x '0...0' to the right;

3) for negative numbers, <<x equals to losing first x bits and add x '0...0' to the right, but make sure the most signaficant bit is '1' afterwards;

4) for negative numbers, >>x equals to losing last x bits and add x '1...1' to the left;

So no matter how you apply >> to -1 (1...1), it is still -1 (1...1).

[ December 17, 2002: Message edited by: Don Liu ]

Prashant Neginahal

Ranch Hand

Posts: 76

posted 14 years ago

Hey David!!

I think u r statement

is not correct.

Ofcourse it true for >>(right shift operators).But i am talking about >>>(unsigned right operator).So for >>> , incoming left bits are filled with 0's not with 1's coz it is unsigned.

If this is true i was wondering how the fallowing statement is correct.

-1>>>32=-1;...?

Hope somebody will answer.But pls give full explaination with bit wise represenation.

-------------------------------

Sharing is better way of Learning

Prashant

I think u r statement

**Secondly, the '>>>' operation pushes everything to the right and fills with 1's**is not correct.

Ofcourse it true for >>(right shift operators).But i am talking about >>>(unsigned right operator).So for >>> , incoming left bits are filled with 0's not with 1's coz it is unsigned.

If this is true i was wondering how the fallowing statement is correct.

-1>>>32=-1;...?

Hope somebody will answer.But pls give full explaination with bit wise represenation.

-------------------------------

Sharing is better way of Learning

Prashant

--------------<br />Prashant<br />SCJP-91%

John Lee

Ranch Hand

Posts: 2545

Benjoe Reyes

Ranch Hand

Posts: 109

posted 14 years ago

>>> is an unsigned operator so -1 >>> n gives n zeros at the left of the remaining ones (32-n)...

the reason -1>>>32 = -1 is that it is really:

-1>>>(32%32) so it is -1>>>0 so....... nothing happened

32 for int, 64 for long

i think i saw it in Complete Java 2 Certification guide by Roberts, Earnest and Heller...

[ December 17, 2002: Message edited by: Benjoe Reyes ]

[ December 17, 2002: Message edited by: Benjoe Reyes ]

the reason -1>>>32 = -1 is that it is really:

-1>>>(32%32) so it is -1>>>0 so....... nothing happened

32 for int, 64 for long

i think i saw it in Complete Java 2 Certification guide by Roberts, Earnest and Heller...

[ December 17, 2002: Message edited by: Benjoe Reyes ]

[ December 17, 2002: Message edited by: Benjoe Reyes ]

John Lee

Ranch Hand

Posts: 2545

posted 14 years ago

Anyway, Prashant, you didn't mention what '-1' is?

Assume the number of bits is n, then,

-1 >>> 32 = 2^( n - mode(32,n) ) - 1, when mode(32,n) != 0 ;

-1 >>> 32 = - 1, when mode(32,n) = 0 ;

Originally posted by Prashant Neginahal:

Hello Everybody,

How -1>>>32 is equal to -1.?Pls explain.

------------

Prashant

Anyway, Prashant, you didn't mention what '-1' is?

Assume the number of bits is n, then,

-1 >>> 32 = 2^( n - mode(32,n) ) - 1, when mode(32,n) != 0 ;

-1 >>> 32 = - 1, when mode(32,n) = 0 ;

Barry Gaunt

Ranch Hand

Posts: 7729

posted 14 years ago

As I said way up there somewhere:

-1 is an

'>>>' is the

It fills from the left with zeros.

Shifting an

So -1 >>> 32 is -1 >>> 0 is -1.

Note: In case the lefthand operand is a

-Barry

[ December 19, 2002: Message edited by: Barry Gaunt ]

Anyway, Prashant, you didn't mention what '-1' is?

As I said way up there somewhere:

-1 is an

**int**literal, -1L is a

**long**64 bit integer literal.

'>>>' is the

*logical*right shift operator.

It fills from the left with zeros.

Shifting an

**int**causes only the low order 5 bits of the righthand operand to be used. Taking a modulus not always the right thing to do because of the case of the righhand operand being negative.

So -1 >>> 32 is -1 >>> 0 is -1.

Note: In case the lefthand operand is a

**long**the low order six bits of the righthand operator must be used. So try -1L >>> 32 for fun.

-Barry

[ December 19, 2002: Message edited by: Barry Gaunt ]

Getting someone to think and try something out is much more useful than just telling them the answer.

John Lee

Ranch Hand

Posts: 2545

posted 14 years ago

The question was originally from Prashant, so I have to go back to the root question.

Try it, it is fun. In case you got a different result, please let me know.

[ December 19, 2002: Message edited by: Don Liu ]

Originally posted by Barry Gaunt:

As I said way up there somewhere:

-1 is anintliteral, -1L is along64 bit integer literal.

The question was originally from Prashant, so I have to go back to the root question.

'>>>' is thelogicalright shift operator.

It fills from the left with zeros.

Shifting anintcauses only the low order 5 bits of the righthand operand to be used. Taking a modulus not always the right thing to do because of the case of the righhand operand being negative.

So -1 >>> 32 is -1 >>> 0 is -1.

Note: In case the lefthand operand is alongthe low order six bits of the righthand operator must be used. So try -1L >>> 32 for fun.

-Barry

[ December 19, 2002: Message edited by: Barry Gaunt ]

Try it, it is fun. In case you got a different result, please let me know.

[ December 19, 2002: Message edited by: Don Liu ]

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |