2

Hi,I have a question

If write(0x01234567) is called on an instance of output stream, what will be written to the destination of the stream?

a. The bytes 0x01, 0x23, 0x34, 0x45, and 0x67, in that order.

b. The bytes 0x67, 0x45, 0x34, 0x23, and 0x01, in that order.

c. The byte 0x01.

d. The byte 0x67.

e. None of the above.

I am getting "D" but answer "C"?, why? please explain

Silviu Burcea
  • 5,103
  • 1
  • 29
  • 43
  • 1
    Explain your reasoning. Why "D"? – PM 77-1 Dec 12 '16 at 21:49
  • 5
    [`OutputStream.write(int)`](http://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#write(int)) says (in part) *The byte to be written is the eight low-order bits of the argument `b`. The 24 high-order bits of `b` are ignored.* – Elliott Frisch Dec 12 '16 at 21:49
  • 3
    "I am getting "D" but answer "C"" - what do you mean? Do you mean you believe it should be D, but the test says it's C, or the other way round? – Jon Skeet Dec 12 '16 at 21:53
  • 1
    And what does this have to do with C++11? – Jon Skeet Dec 12 '16 at 21:53
  • The HotSpot JVM is implemented in C++, perhaps because it's a low-level IO question OP is wondering about the implementation and endianness. – rbrtl Dec 12 '16 at 22:02
  • 1
    @ElliottFrisch So the least significant byte, `0x67`, would be written? – qxz Dec 13 '16 at 06:45

1 Answers1

0

The output can vary depends on machine, support byte ordering big endian or little endian. I think you ran the program on the machine that support little endian byte ordering that's why you getting 67.

Learn More: https://www.geeksforgeeks.org/little-and-big-endian-mystery/

Milan
  • 179
  • 1
  • 8
  • Java is _never_ affected by platform endianness. And even in other languages like C and C++ it matters only when you pass a reference or pointer to memory, not a value like the one in the Q. – dave_thompson_085 Jul 02 '19 at 16:12