1

I am using java's built in Integer.toBinaryString(myInt) to convert to a binary string and then I am converting that 32-bit string into an 8-bit string.

My issue lies in that when converting the number back into a signed-integer I lose the sign.

Example:

My Int = -5.

Binary representation = 11111011.

Converting back to integer: 251.

Some of my code:

//Converts an integer to 8-bit binary.
public static String convertTo8BitBinary(int myNum){
    String intToConv = Integer.toBinaryString(myNum);
    //the number is less than 8-bits
    if(intToConv.length()<8){
        String append="";
        for(int i = 8 - intToConv.length(); i>0;i--){
            append += "0";
        }
        intToConv = append+intToConv;
    //the number is more than 8 bits
    }else {
        intToConv = intToConv.substring(intToConv.length() - 8, intToConv.length());
    }
    return intToConv;
}

//Converts an 8-bit binary string to an integer.
public static int convertToIntegerFromBinary(String b){
    return Integer.parseInt(b,2);
}

Any ideas how I can retain the sign? Does the Integer.parseInt(b,2) not work for signed integers? Is there a radix that does work for signed binary?

1

2 Answers 2

1

While representing singed integers, basic machine architecture would consider the uppermost bit for sign. When the uppermost bit set to '1', then its a negative number, otherwise positive number, more accurate integer instead of number. In you case you would be considering the least 8 bits out of 32 bits. But sign bit present at 32nd position hence loosing the sign

You could do bit AND operation as follows: my_32_bit_number & covert this to int (1000 0000 0000 0000 0000 0000 1111 1111) will give you required 8 bit-number

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

2 Comments

Is there no way to work with the 8th bit being the sign?
Instead of Integer, make use of Byte (apt for 8-bit integers) and consider only 7 bits. Set the 8th bit (MSB-Most Significant bit) to sign of the previous number i.e, the 32th bit.
0

You should manage the most significant bit which is a marker for the sign.

The code below do exactly what you do, but works on (N-1) bits (for 8bits strings, N = 8), then appends 0 or 1 at the start of the result string in order to keep the sign.

//Converts an integer to n-bit binary.
public static String convertToNBitBinary(int myNum, int nBitSigned){
   int nBitUnsigned = nBitSigned -1;

   if(myNum > Math.pow(2,nBitUnsigned) || myNum <= -Math.pow(2,nBitUnsigned) ){
      return "OutOfBound";
   }

   String intToConv = Integer.toBinaryString(Math.abs(myNum));

   //the number is less than nBitUnsigned
   if(intToConv.length()<nBitUnsigned){
      String append="";
      for(int i = nBitUnsigned - intToConv.length(); i>0;i--){
         append += "0";
      }
      intToConv = append + intToConv;
   }else { //the number is more than 8 bits
      intToConv = intToConv.substring(intToConv.length() - nBitUnsigned);
   }
   intToConv = (myNum <0?"1":"0") + intToConv;
   return intToConv;
}

The second method has to be corrected for the same reason :

//Converts an N-bit binary string to an integer.
public static int convertToIntegerFromBinary(String b){
   String number = b.substring(1);

   return (b.charAt(0) == '0'?1:-1)*Integer.parseInt(number, 2);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.