Question: I have tried to convert **248** to binary and then convert it to two's compliment and finally convert it back to decimal.

Problem: result is **`8`** not **`-8`**

![](https://i.stack.imgur.com/8PMOz.png)

Answer: 
Convert the hex number digit by digit to a 4-bit binary, for example (A) to (1010) and put then together. Now you can calculate the 2‘s complement as explained below.   

You'll see you need at least 9 bits for (F8)16, so if you're only working with a 8-bit variable for instance, you might encounter issues.  

**Calculating the 2‘s complement:**

1. (248)10 = (0 1111 1000)2.
2. Complement all the bits:  

(1 0000 0111)2.
3. Add 1:  

(1 0000 1000)2.
This will give you (-248)10. Notice that if you're only looking at the last 8 bits (0000 1000)2, the bit string evaluates to 8. I hope this helps in some way.
Java specifically keeps a single byte in the min/max range of `-128 to 127`.   

A value like **248** (hex: `0xF8`) is already **stored as a Short** (2 bytes or 16 bits) in Java.

`[0000 0000] [1111 1000]` = **248** in Java. But you need only `[1111 1000]` for **-8**.

You'd think *"If I remove starting byte `[0000 0000]` then it works..??"* No because now you have 1 byte left and that remaining byte with `[1111 1000]` alone cannot make **248** since Java's maximum for a single byte is **127**.

**Solution**:  

In those available 16 bits, replace all bits leading up to your value with `1`.

Visually your bits `[0000 0000] [1111 1000]` = **248** must become as `[1111 1111] [1111 1000]` = **-8**.

One quick way to achieve the result is through this logic `result = (-256) | ( x );`.

*Code example:*   

```
int x = 248; //# your input value from reading

int result = (-256) | ( x );

System.out.println("value is " + result ); //# gives: value is -8

```