Binary Operations

M

MarcoGT

Hi,

i had to do some mathematical operations with binary numbers...; but i see
tha this is not implmented in Java; where can i find something for example?

Thanks.

Bye
 
C

Collin VanDyck

MarcoGT said:
Hi,

i had to do some mathematical operations with binary numbers...; but i see
tha this is not implmented in Java; where can i find something for example?

Thanks.

Bye

You can certainly do binary operations.

You have the standard bitwise AND (&) and the bitwise OR (|) that you would
have in other languages. Also, the Integer class let's you take any int and
give you the binary string representation:

int x = 7;
Integer.toBinaryString(x); // this returns "111"

So what exactly is it you are trying to do? Please try and give more
information.

-CV
 
M

Michael Borgwardt

MarcoGT said:
i had to do some mathematical operations with binary numbers...; but i see
tha this is not implmented in Java;

What "mathematical operations" are you talking about? Java definitely has all
the usual mathematical operations (+, -, *, /), which have nothing to do with
the numbers' base (which is a property of their representation, not the
numbers itself). It also internally uses a binary format and offers
appropriate bitwise operations (AND, OR, etc.) and finally you can easily
convert between numerical variables and a binary String reprentations.

So what exactly do you want to do and why do you believe it's not implemented
in Java?
 
M

MarcoGT

Collin VanDyck said:
So what exactly is it you are trying to do? Please try and give more
information.

I need to do sums ed subractions;

for example i tried to make 111+1 and the result was 112...why?

Thanks
 
C

Christophe Vanfleteren

MarcoGT said:
I need to do sums ed subractions;

for example i tried to make 111+1 and the result was 112...why?

Thanks

Because 111 + 1 is 112 ofcourse :)

Seriously, all math in java is base 10 by default.

What you can do is retrieve your base 2 numbers in String form, convert them
to an int using Integer.parseInt("111",2), and add those ints. To get the
resulting base 2 representation, use Integer.toBinaryString on the sum, as
someone has mentioned before.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top