adding binary numbers in java please help!!

Hello all, im a total newbie to java and i need your help urgently, i have two variables

that are integers, that store binary numbers i.e.

int tmpIntOne, tmpIntTwo;

tmpIntOne = 1010;

tmpIntTwo = 1110;

i want to add these numbers together and return a binary result so far when you add them together you get:

2120

however i would like a binary result so that

result = tmpIntOne + tmpIntTwo;

= 11000

Thank you.

[477 byte] By [trscookiea] at [2007-10-2 13:20:55]
# 1
> int tmpIntOne, tmpIntTwo;> > tmpIntOne = 1010;> tmpIntTwo = 1110;> > 1010 = 10;1110 = 14;10 + 14 = 24System.out.println(Integer.toBinaryString(24));
rkippena at 2007-7-13 10:56:29 > top of Java-index,Other Topics,Algorithms...
# 2

> Hello all, im a total newbie to java and i need your

> help urgently, i have two variables

> that are integers, that store binary numbers i.e.

>

> int tmpIntOne, tmpIntTwo;

>

> tmpIntOne = 1010;

> tmpIntTwo = 1110;

These are not binary numbers. These are decimal numbers whose digits consist of ones and zeros. If you want to interpret the characters "010" as a binary number, then do

int tmp1 = Integer.parseInt("1010", 2);

etc.

> i want to add these numbers together

int result = tmp1 + tmp;

> and return a

> binary result

Adding two ints will give an int. Int's are always binary. If you want to display it as a binary String, as opposed to the usual decimal String, then, as suggested, use toBinaryString.

jverda at 2007-7-13 10:56:29 > top of Java-index,Other Topics,Algorithms...