• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

bitwise operator question

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello all,
i found this

# Please select true statement(s) for shift operators.

1. >>> operator always produces a positive number.
2. >> always produces a positive number for a positive number.
3. >> always produces a negative number for a negative number.
4. None of the above

i answered 1,2,3 but correct answer was only 1

anyone could explain me why?

thanks and regards
marco
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't even think 1 is correct.

If the left operand of an unsigned right shift (>>>) is a negative int, and the right operand is some multiple of 32, then the result will be negative.
[ April 20, 2006: Message edited by: marc weber ]
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Mark about number 1.

Also on 2, a positive integer could get shifted to 0.

But what is a counterexample to 3?
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For choice 1 if you do the byte shift on type less than int you can't assure of a positive number.
Consider byte b = -127;
The bit representation is 1000 0001 in 2's complement.
When you do the b = (byte) (b >>> 1) the steps performed are

  • byte is promoted to int [11111111 11111111 11111111 10000001]
  • b >>> 1; [01111111 11111111 11111111 11000000]
  • b = (byte) (b>>>1); 1100 0000
  • Final value is -64


  • Hence its still a negative number.

    Second option may not hold true when you cast to lower precision
    consider int i = 256;
    byte b = (byte) (i >> 1);

  • int i = 256; [00000000 00000000 00000001 00000000]
  • i>>1; [00000000 00000000 00000000 10000000]
  • byte b = (byte) (i >> 1); [1000 0000]
  • Final value is -128



  • Third option also doen't hold in downward casting
    Consider int i =
    byte b = (byte) (i >> 1);

  • int i = -512; [11111111 11111111 11111110 00000000]
  • i >> 1; [11111111 11111111 11111111 00000000]
  • byte b = (byte) (i >> 1); [00000000]
  • Final value is 0



  • Hence the answer should be none of the above.
    But can anyone inform whether we have to consider the casting also in this kind of situtaion.
    But for SCJP 5 you are free of bit shift.
    [ April 20, 2006: Message edited by: Shaliey Gowtham ]
     
    marc weber
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Originally posted by Shaliey Gowtham:
    ... But can anyone inform whether we have to consider the casting also in this kind of situtaion...


    According to section 15.19 of the JLS...

    Binary numeric promotion ... is not performed on the operands; rather, unary numeric promotion ... [to at least int] is performed on each operand separately. The type of the shift expression is the promoted type of the left-hand operand.


    Unless specifically mentioned, I would not assume explicit casting to any type.
     
    Cob is sand, clay and sometimes straw. This tiny ad is made of cob:
    the value of filler advertising in 2020
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic