When I say that 0b11111111 is a byte in java, it says " cannot convert int to byte," which is because, as i understand it, 11111111=256, and bytes in java are signed, and go from -128 to 127. But, if a byte is just 8 bits of data, isn't 11111111 8 bits? I know 11111111 could be an integer, but in my situation it must be represented as a byte because it must be sent to a file in byte form. So how do I send a byte with the bits 11111111 to a file(by the way, this is my question)? When I try printing the binary value of -1, i get 11111111111111111111111111111111, why is that? I don't really understand how signed bytes work.
Asked
Active
Viewed 9,472 times
3
-
1Do you want a raw byte value or a string written to the file? There are solutions for either case. – Ted Hopp Jul 18 '16 at 02:21
-
I would like raw bytes written the file I believe. Isn't a text file with a bunch of ones and zeros the same as as a file with raw bytes though? – user2350459 Jul 18 '16 at 02:26
-
1Not at all. If you write a single byte as raw data to a file, the file size will be 1 byte; if you write a string representation of the bit values, the file size will be 8 bytes. See [here](http://www.trustingeeks.com/difference-between-binary-and-text-files/) or [here](https://www.nayuki.io/page/what-are-binary-and-text-files) for discussions of the difference between binary and text files. – Ted Hopp Jul 18 '16 at 02:32
-
Oh ok, awesome. Thanks for answering all my questions, I really appreciate it. – user2350459 Jul 18 '16 at 02:34
2 Answers
11
You need to cast the value to a byte:
byte b = (byte) 0b11111111;
The reason you need the cast is that 0b11111111
is an int
literal (with a decimal value of 255) and it's outside the range of valid byte
values (-128 to +127).

Ted Hopp
- 232,168
- 48
- 399
- 521
-
when I do this and then call print out Integer.toBinaryString(b), it prints out 11111111111111111111111111111111, not 11111111. Why is this " byte" printing out more than 8 bits, and when I write it to a file, will it send 11111111? – user2350459 Jul 18 '16 at 02:15
-
1@user2350459 - That's because as a `byte`, the bit pattern is indeed `-1`. When you call `toBinaryString`, it expands the argument from a `byte` to an `int`, so you get the binary representation of the **`int`** value -1. – Ted Hopp Jul 18 '16 at 02:19
-
why would 00000000000000000000000011111111 and 11111111 be different numbers, and why are 11111111111111111111111111111111 and 11111111 equal to each other? – user2350459 Jul 18 '16 at 02:21
-
1@user2350459 - Because an `int` value that is all 1 bits and a `byte` value that is all 1 bits are both the value -1. When you compare them with `==`, the byte value gets widened to an `int` before the comparison is done. See [this thread](http://stackoverflow.com/q/11380062/535871) for a related discussion. – Ted Hopp Jul 18 '16 at 02:24
1
Java allows hex literals, but not binary. You can declare a byte with the binary value of 11111111 using this:
byte myByte = (byte) 0xFF;
You can use hex literals to store binary data in ints and longs as well.
Edit: you actually can have binary literals in Java 7 and up, my bad.

Neatname
- 124
- 6