3

Okay, A friend helped me with this code a little bit. I understand what everything else does and why it does it except for one thing. Where is this 128 coming from?? Also, this program runs, pulls the string from the file, converts it to binary, but takes all of the spaces out, so that when you re convert the binary back to the string, it is all one word. So what is the 128 and what can I do to keep the spaces?

/******************************* *I fixed it, Thanks for your help guys! * *I have changed the code so you can see how I fixed it. * *******************************/

         public static void main(String[] args) {

          String text = "My string to binary works too";

          byte[] bytes = text.getBytes();
          StringBuilder binary = new StringBuilder();

          for (byte b : bytes){
            int val = b;
          for (int i = 0; i < 8; i++){
            binary.insert(0, (val & 1) == 0 ? 0 : 1);
            val >>>= 1;
          }
            binary.insert(0, ' ');
         System.out.print(binary);
      }



      }

}

9
  • Highest binary value is 128...! So it might come Commented Oct 27, 2013 at 4:13
  • Highest binary value is not 128?? Commented Oct 27, 2013 at 4:16
  • the highes slot value in a byte is 128. but i dont understand what the 128 is doing here Commented Oct 27, 2013 at 4:18
  • Re your question about keeping the spaces, are you talking about the spaces between the binary bytes in your output or the spaces between words in the original text? Note that an ASCII space in binary is 0010000 so you should see some of those in your binary output. Commented Oct 27, 2013 at 4:23
  • 1
    @Turix next() from Scanner using whitespace as a delimiter, which is why he's losing the spaces. Commented Oct 27, 2013 at 4:26

3 Answers 3

1

128 is 2^7 power, which means in binary it is 10000000. Bytes consist of 8 bits. So on the line where the 128 is being used, you are doing a bitwise AND to get the highest bit. (Then on the next line, you shift the value to one bit to the left and repeat, so you successively get each bit value from left to right.)

Sign up to request clarification or add additional context in comments.

15 Comments

Okay, that makes total sense. Thanks
What about byte's sign?
@Joey For reference, you lose the spaces because you're using next() from Scanner, which uses white-space as a delimiter.
@Basilevs The most significant bit of a number is the sign bit (usually). But in this case, he is just converting characters to binary bytes, not trying to get a numeric value for them. (And assuming he's working with ASCII characters, the most significant bit will always be 0 anyway.)
@Basilevs To be clear, his code will work (that is, print the correct binary representations of the characters) whether they are ASCII or not, irrespective of the signed/unsigned status of the bytes.
|
0

As others have said, 128 is just a bit mask of 1 in the MSB.

Following the logic through if you have a byte that is 10011001:

10011001 & 10000000 = 10000000 != 0
11011001 << 1 = 00110010
00110010 & 10000000 = 00000000 == 0

and so on...

Here's alternative logic that achieves the same thing but reversed (mask in the LSB) and perhaps makes more immediate sense:

int val = b;
for (int i = 0; i < 8; i++)
{
    binary.insert(0, (val & 1) == 0 ? 0 : 1);
    val >>>= 1;
}
binary.insert(0, ' ');

Following this logic through you have:

10011001 & 00000001 = 00000001 != 0
11011001 >>> 1 = 01101100
01101100 & 00000001 = 00000000 == 0

and so on...

3 Comments

You're welcome. : ) Although I understand the use of 128, using 1 makes more sense to me and works for any word length. 128 only works for a byte.
Instead of iserting a space someone could recommend not to use Scanner altogether. InputStream is there exactly for this.
@Basilevs Yeah it's true InputStream is a more direct approach. Using raw FileInputStream would also negate the need for a String being read at all since the OP could just directly use the bytes from the read.
0

The 128 is an 1000 0000 in binary, so it is the most significant bit (MSB) of byte. MSB is the fisrt bit from the left. Since the code prints bits from MSB to LSB, the every loop iteration prints 1 or 0 depending on result of AND (&) with value having only MSB bit setted (128), and shifts value to left by one bit.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.