• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

value & 0xff

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is a question from KAM
Q> When will the method return false?
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The key is that the hex literal 0xFF is still an int..that is, it takes up 32 bits. That makes it look like this in binary:
00000000 00000000 00000000 11111111
When you do a bitwise AND with this value, it is going to mask all but the lowest 8 bits of the number.
InputStream.read() always returns an int, in the range of 0-255, UNLESS the end of the stream was reached, in which case it returns -1.
In binary, 0 is, well, all zeros, and 255 looks like this:
00000000 00000000 00000000 11111111
But a -1 looks like this
11111111 11111111 11111111 11111111
When you do a bitwise AND of 0xFF and any value from 0-255, the result is the exact same as the value. However, if you do:
-1 & 0xFF
you get
00000000 00000000 00000000 11111111, which does NOT equal the original value of -1.
SO that is why they say that method will only return false when the value returned by the call to read() is -1, signifying the end of the stream.
ALSO, note you have a typo in your method. The return statement should read:
return value == (value & 0xFF);
Note the use of the EQUALITY, not the ASSIGNMENT operator.
Your syntax as written works for C++ but is illegal in java.
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Samith
This is a very interesting question. Let's take attention on a
value = value&0xFF
0xFF = 11111111 ( 8 bits )
and the rule of & operator is
1&0=0
1&1=1
And the corollary from this is that
value = value&0xFF will be true if TYPE OF VALUE IS BYTE(!!!) . Example
11111111
&
01110010
--------
01110010

If type of value is not byte, (for example int )then length of binary representation of value will be more than 8 symbols. Sample

value = 1111011011101101
0xFF = 0000000011111111
result will be 0000000011101101 !!!
That's why answer is
'result will be false when value is grater than 256'
Jamal
 
Samith Nambiar
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanx Rob & Jamal ... that clears my doubt.
can anyone pls guide me to some source where i can learn more about bitwise masking, its implication unde different circumstances and its practical use
thankx
/SAmith
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic