• 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

read() in File class

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the following code, under which circumstances will the method return false?
public static boolean test(InputStream is) throws IOExecption{
int value =is.read();
return value==(value& 0xff);
}
Select all valid answers.
A) a character of more than 8 bits was read from the stream
B) An I/O error occurred.
C) Never
D) The end of the input was reached in the input stream
Answer is D.
As far as I know, the end of the input stream when reached by read, is -1, so return will be
-1==(-1&-1) that will return true, am I right?
Thanks
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case, the &Oxff is called a "mask." You only care about the last 8 bits of the value, so you do a bitwise & (and) with 8 bits that are all set to '1'.
When the read returns a value of -1 the bits in value are:
11111111111111111111111111111111 (32 bits)
when you take this and & it with OxFF you get
00000000000000000000000011111111
When you compare them with == you can see that they are NOT equal, so you return false. This only happens when read() returns -1. any other time, the comparison will be true.
For example, if read() returns the number 8:
value is
0000000000000000000000000001000
when you "and" (&) this with OxFF you get the same number, so you return true.

Hope this helps.
Rob
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Angela -
I was sure that you were right but I ran this

and got 'false'.
Then I ran some other tests:

It seems that 0xff is getting promoted to an int, 0x000000ff, before computation.
So, when doing an & operation the byte gets promoted by shifting in zeros. According to RHE, if we did '0xff >> -1', for example, ones would get shifted in. I guess the sign of the number being promoted is preserved only when the operator specifically dictates a signed operation. Does anyone know if that's right?
Thanks for the good question.
Sylvia
 
sylvia weller
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd like to correct what I said in my original reply. I said that bits (0 or 1) got _shifted_ in to promote a byte to an int. This is wrong. _Shifting_ occurs only on integral types in >>, <<, or >>> operations.
Is it accurate to say that promoting a byte to an int involves padding the byte with zeros to fill the high order bits? And that zeros are always used in promoting a value, regardless of the sign of the value?
Sorry to be misleading.
Sylvia
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Numeric promotion always preserves the sign of the operand.
This is done by sign-extension. So if the sign of the operand is postive, zeros are used to pad the high-order bits, and if the sign is negative, ones are used.
Rob
 
sylvia weller
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi once again,
I hope I didn't confuse anyone with my very wrong ideas on this subject in my first post. Just trying to think things through and learn.
Rob, thanks for your clear replies. One last correction to my first post - NO promotion is applied to 0xff, it is an integer literal.
Sylvia
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic