Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!

# Convert Integer to byte value's formula

Soe San
Greenhorn
Posts: 6
when I convert Integer value to byte, compiler output is minus value and sometimes positive.
Please somebody answer for the FORMULA for that?

BitUnsignedTwo complement value
0000 000022
0111 1111127127
1000 0000128-128
1111 1111255-1
1000 0010130-126

I want to know the formula for that and Please Could somebody answer for the FORMULA for that?

Eg.2) int i = 999;
byte b = (byte) i;
System.out.println(b);
Out put is . -25

Eg.2) int i = 2147482647;
byte b = (byte) i;
System.out.println(b);
Out put is . 23

Les Morgan
Rancher
Posts: 710
17
If you have the left most bit as a negative, the number is considered negative, unless you are using an unsigned. When you convert an unsigned you have to take into account that normally that leading bit is considered a negative indicator, but in an unsigned it is just another power of 2.

Here is a link describing 2's compliment: 2's compliment explanation

fred rosenberger
lowercase baba
Bartender
Posts: 12264
36
• 1
I think you mean "If you have the left most bit as '1'..."

Soe San
Greenhorn
Posts: 6
fred rosenberger wrote:I think you mean "If you have the left most bit as '1'..."

Yes sir, i want to know the formula.
As i did , my formula is only correct 0 to 999

Eg.1) 255 --> 1111 1111

(2^N-1) =2^7 = 128

255-128
= 127
=127-128
=-1 (Answer)

Soe San
Greenhorn
Posts: 6
• 1
Les Morgan wrote:If you have the left most bit as a negative, the number is considered negative, unless you are using an unsigned. When you convert an unsigned you have to take into account that normally that leading bit is considered a negative indicator, but in an unsigned it is just another power of 2.

Here is a link describing 2's compliment: 2's compliment explanation

Thanks you Sir.
I got it.

Roel De Nijs
Sheriff
Posts: 10662
144
Hi Soe San,

First of all, a warm welcome to CodeRanch!

Soe San wrote:Please somebody answer for the FORMULA for that?

If you are preparing for the OCAJP certification exam, you don't need to know that formula as it is not one of the exam topics.

Soe San wrote:I want to know the formula for that and Please Could somebody answer for the FORMULA for that?

In order to be able to calculate the actual value, you need to know three important concepts:
1/ binary numbers (the binary system is used internally by computers and computer-based devices to e.g. represent values like 999)
2/ what happens if you cast a value from e.g. int to byte
3/ Two's complement (so you can calculate the actual value for both positive and negative numbers).

Let's have a look at your first example:So the value 999 is assigned to an int. As you probably know, an int is a 32-bit signed data type, so 999 is represented like 0000 0000 0000 0000 0000 0011 1110 0111. The left-most bit (in bold) indicates that this number is a positive value. A byte is a 8-bit signed data type. Now when you cast from an int to a byte, only the 8 righ-most bits are copied and the rest of the bits is discarded. So the value of b is equal to 1110 0111. The left-most bit (in bold again) indicates that this number is a negative value. Using two's complement you can calculate the actual value.

There's another alternative to calculate the actual value A byte is a 8-bit signed data type, so it has a range from -128 to 127 (and there are 256 possible values). To calculate the actual value you can subtract (or add) 256 from the value until it falls into the range. So let's try with 999So the values 743, 486, and 231 are all outside of the range of a byte, so you subtract 256 from the remaining value. The value -25 fits into the range of a byte, so that's the value you were looking for

Needless to say that with your second example, you need a boatload of subtractions until you will reach the actual byte value. But you could always write a little application to help you If you execute the above code, the output will be "result: 23 (8388604)". So you almost needed 8.4 million subtractions

Hope it helps!
Kind regards,
Roel

Roel De Nijs
Sheriff
Posts: 10662
144
Soe San wrote:when I convert Integer value to byte, compiler output is minus value and sometimes positive.

I almost forgot to nitpick about this statement In your code snippets you are not converting Integer value to byte, but you are converting an int value to byte. Please note that in Java there is a difference between int (a primitive value) and Integer (a primitive wrapper class). In fact, if you would try to cast an Integer value to a byte, you'll get a compiler error as you can't cast a reference type to a primitive type. Illustrated in this code snippet

Hope it helps!
Kind regards,
Roel

Soe San
Greenhorn
Posts: 6
Roel De Nijs wrote:
Soe San wrote:when I convert Integer value to byte, compiler output is minus value and sometimes positive.

I almost forgot to nitpick about this statement In your code snippets you are not converting Integer value to byte, but you are converting an int value to byte.

Yes! Yes! Yes!
That is my Carless mistake Sir,
Soe San

Soe San
Greenhorn
Posts: 6
Roel De Nijs wrote:In order to be able to calculate the actual value, you need to know three important concepts:
1/ binary numbers (the binary system is used internally by computers and computer-based devices to e.g. represent values like 999)
2/ what happens if you cast a value from e.g. int to byte
3/ Two's complement (so you can calculate the actual value for both positive and negative numbers).

Oppp! Yes! Yes!
Thanks you! Sir