Binary Operations

Discussion in 'Java' started by MarcoGT, Feb 27, 2004.

  1. MarcoGT

    MarcoGT Guest

    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
     
    MarcoGT, Feb 27, 2004
    #1
    1. Advertising

  2. "MarcoGT" <> wrote in message
    news:c1odij$1m04i7$-berlin.de...
    > 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
     
    Collin VanDyck, Feb 27, 2004
    #2
    1. Advertising

  3. MarcoGT wrote:
    > 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?
     
    Michael Borgwardt, Feb 27, 2004
    #3
  4. MarcoGT

    MarcoGT Guest

    "Collin VanDyck" <> ha scritto nel messaggio
    news:W3P%b.9372043$...
    > "MarcoGT" <> wrote in message
    > news:c1odij$1m04i7$-berlin.de...
    > 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
     
    MarcoGT, Feb 28, 2004
    #4
  5. MarcoGT wrote:

    >
    > "Collin VanDyck" <> ha scritto nel messaggio
    > news:W3P%b.9372043$...
    >> "MarcoGT" <> wrote in message
    >> news:c1odij$1m04i7$-berlin.de...
    >> 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


    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.


    --
    Kind regards,
    Christophe Vanfleteren
     
    Christophe Vanfleteren, Feb 28, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fangs
    Replies:
    3
    Views:
    9,801
    darshana
    Oct 26, 2008
  2. Jeremy Robbins

    Binary File Operations

    Jeremy Robbins, Jul 14, 2004, in forum: Perl
    Replies:
    0
    Views:
    505
    Jeremy Robbins
    Jul 14, 2004
  3. Jesus M. Salvo Jr.
    Replies:
    2
    Views:
    4,246
    robert
    Feb 11, 2006
  4. Marc Schellens
    Replies:
    8
    Views:
    3,022
    John Harrison
    Jul 15, 2003
  5. Leslaw Bieniasz
    Replies:
    4
    Views:
    356
Loading...

Share This Page