# Convert Integer to byte value's formula

Soe San

Greenhorn

Posts: 6

posted 8 months ago

when I convert Integer value to byte, compiler output is minus value and sometimes positive.

Please somebody answer for the FORMULA for that?

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

Please somebody answer for the FORMULA for that?

Bit | Unsigned | Two complement value |
---|---|---|

0000 0000 | 2 | 2 |

0111 1111 | 127 | 127 |

1000 0000 | 128 | -128 |

1111 1111 | 255 | -1 |

1000 0010 | 130 | -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

posted 8 months ago

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

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

Out on HF and heard nobody, but didn't call CQ? Nobody heard you either. 73 de N7GH

Soe San

Greenhorn

Posts: 6

Soe San

Greenhorn

Posts: 6

posted 8 months ago

Thanks you Sir.

I got it.

- 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.

posted 8 months ago

Hi Soe San,

First of all, a warm welcome to CodeRanch!

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.

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.

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

There's another alternative to calculate the actual value A

Needless to say that with your second example, you need a boatload of subtractions until you will reach the actual

Hope it helps!

Kind regards,

Roel

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`. The left-most bit (in bold) indicates that this number is a positive value. A`**0**000 0000 0000 0000 0000 0011 1110 0111`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`. 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.`**1**110 0111There'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 forNeedless 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 subtractionsHope it helps!

Kind regards,

Roel

posted 8 months ago

I almost forgot to nitpick about this statement In your code snippets you are not converting

Hope it helps!

Kind regards,

Roel

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

posted 8 months ago

Yes! Yes! Yes!

That is my Carless mistake Sir,

Soe San

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 convertingIntegervalue tobyte, but you are converting anintvalue tobyte.

Yes! Yes! Yes!

That is my Carless mistake Sir,

Soe San

Soe San

Greenhorn

Posts: 6

posted 8 months ago

Oppp! Yes! Yes!

Thanks you! Sir

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.inttobyte

3/ Two's complement (so you can calculate the actual value for both positive and negative numbers).

Oppp! Yes! Yes!

Thanks you! Sir