This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Please what is this

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please how will this be false ans why please?

public class ObjectTest {

public static void main(String[] args) {
System.out.println(0 < 1 << 2 >> 1 >>> 2);
}
}
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The boolean expression is basically "0 < something," where "something" is a series of bit-shifting operations, 1 << 2 >> 1 >>> 2. Shift operators are included in the 1.4 exam, but not the 1.5 exam. Shift operators "are syntactically left-associative (they group left-to-right)." Ref: JLS - 15.19 Shift Operators.

So (0 < 1 << 2 >> 1 >>> 2) evaluates as (0 < (((1 << 2) >> 1) >>> 2)).

First, consider (1 << 2). In binary, 1 is...

0000 0000 0000 0000 0000 0000 0000 0001

Shifted to the left by 2, this is...

0000 0000 0000 0000 0000 0000 0000 0100

Next, this quantity shifted to the right by 1 (a signed shift) is...

0000 0000 0000 0000 0000 0000 0000 0010

Next, this quantity shifted to the right by 2 (an unsigned shift) is...

0000 0000 0000 0000 0000 0000 0000 0000

...which is zero. So the boolean expression becomes "0 < 0," which is false.
 
He was giving me directions and I was powerless to resist. I cannot resist this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic