4 Replies Latest reply: Dec 30, 2007 8:19 PM by 800308 RSS

    how to print integer result in binary format

    807601
      When I run following code
      package exam.operators;
      import java.io.*;
      import java.lang.*;
      
      class BitWiseOps
      {
            public static void main(String args[])
            {
               byte firstByte = Byte.parseByte( "10001", 2);
               byte secondByte = Byte.parseByte( "10011", 2);
               int resultXOR = (int)(firstByte ^ secondByte);
               byte finalResult = (byte)(firstByte ^ secondByte);
               finalResult = (byte) (finalResult & 0XFF);
               System.out.println("XOR result = " + finalResult);
            }
      }
      the result on screen is
      D:\sun\exam\operators>D:\sun\sdk\jdk\bin\java exam.operators.BitWiseOps
      XOR result = 2

      but instead i want the result as 00000010 on the screen how do I do that?
        • 1. Re: how to print integer result in binary format
          807601
          you can use
          Integer.toBinaryString(finalResult)
          but it won't have any leading 0s
          • 2. Re: how to print integer result in binary format
            800308
            But it's relatively easy to add leading zeros...
            class FormatTest {
              public static void main(String[] args) {
                String b = Integer.toBinaryString(15);
                System.out.println(("00000000"+b).substring(8-b.length()));
              }
            }
            • 3. Re: how to print integer result in binary format
              807601
              I suspected this question would be coming up next. [*] Nice solution, Keith!

              @javadare, as I said in the other thread, you're only making your job more difficult by using {color:#000080}byte{color} values. Whenever you perform any kind of operation on a {color:#000080}byte{color} or {color:#000080}short{color}, Java converts it to an {color:#000080}int{color}. (That's why the casts to {color:#000080}byte{color} in the fourth and fifth lines of your method are necessary, and why the cast to {color:#000080}int{color} in the third line isn't.) Moreover, sign is preserved in the widening process. That means, if the first bit of the {color:#000080}byte{color} is a one, the first twenty-five bits of the {color:#000080}int{color} will be filled with ones during the conversion. Every time a value gets widened to {color:#000080}int{color} and narrowed back to {color:#000080}byte{color}, whether automatically or by explicit casts, you run the risk of ending up with a different number than you started with. But if you parse the string as an {color:#000080}int{color} in the first place, it will never be interpreted as a negative number (assuming the string meets your stated criterion of being no more than eight characters long). Then you can safely ignore the high-order twenty-four bits, filtering them out as needed using the bit-masking techinque I showed you.



              [*] http://forum.java.sun.com/thread.jspa?threadID=5249974
              • 4. Re: how to print integer result in binary format
                800308
                Ooops!

                Edited by: corlettk on Dec 31, 2007 2:19 AM