• Post Reply Bookmark Topic Watch Topic
  • New Topic

steams question from Mughal cert book

 
George Dee-Brown
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A question on streams and "&".
Mughal page 566 question 18.8 is as follows:
18.8 Given the following code, under which circumstances will the method return false?
public static boolean test(InputStream is) throws IOException {
int value = is.read();
return value == (value & Oxff);
}
the answer given is
"d) the end of input was reached in the input stream"
I thought the answer should be
"c) never"
as the end of input stream will set the variable "value" to -1. The result of -1 & oxff is -1. & is bitwise AND which results in 1 when each corresponding pair of bits is 1.
-1 = 10000001
oxFF = 11111111
& = 10000001
10000001 == 10000001 therefore for an end of file a true will be returned.
Where am I going wrong?
Thanks for you help (in advance!)
George

 
PeiHong Wang
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
0xff=255=00000000 00000000 00000000 01111111;
-1=11111111 11111111 11111111 11111111,
-1&oxff=00000000 00000000 00000000 01111111;
in other words -1&0xff=0xff, so when it reaches the end of file,
value = -1, and value&0xff=0xff; therefor, value==(value&0xff) return false.
haven't written java code for a while. I am not sure, but that was my thought.
 
PeiHong Wang
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, oxff=255=00000000 00000000 00000000 11111111;
but anyway, -1&0xff=0xff.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!