How to add two binary numbers using bitwise AND

Discussion in 'Perl Misc' started by cyrusgreats@gmail.com, Nov 15, 2007.

  1. Guest

    I like to know how to add two binaary numbers using bitwise AND
    something like that:
    $bin_1 = 1011101000001
    $bin_2 = 1000101010101
    $result = 1000101000001

    basically user enter two decimal numbers and code change those numbers
    to binary then using bitwise prints the value/result the convert the
    value to hex numbers.
    Thanks in advance..
    , Nov 15, 2007
    #1
    1. Advertising

  2. On Thu, 15 Nov 2007 15:45:28 -0800, cyrusgreats wrote:

    > I like to know how to add two binaary numbers using bitwise AND
    > something like that:
    > $bin_1 = 1011101000001
    > $bin_2 = 1000101010101
    > $result = 1000101000001


    That's not addition.

    > basically user enter two decimal numbers and code change those numbers
    > to binary then using bitwise prints the value/result the convert the
    > value to hex numbers.
    > Thanks in advance..


    Look up "bitwise and" in perlop.

    Joost.
    Joost Diepenmaat, Nov 16, 2007
    #2
    1. Advertising

  3. Guest

    wrote:
    > I like to know how to add two binaary numbers using bitwise AND
    > something like that:
    > $bin_1 = 1011101000001
    > $bin_2 = 1000101010101
    > $result = 1000101000001


    In addition to the other answers you got, you can do this as strings as
    well:

    print "1011101000001" & "1000101010101";

    The good thing about this is that it is not limited to 32 or 64 bits.
    The bad thing is that if the strings aren't the same length they will
    be aligned in an unintuitive (for numbers) manner.

    And or course the answer is a string rather than a number. Whether that is
    good or bad depends on what you want.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
    , Nov 16, 2007
    #3
  4. Guest

    On Nov 16, 8:47 am, wrote:
    > wrote:
    > > I like to know how to add two binaary numbers using bitwise AND
    > > something like that:
    > > $bin_1 = 1011101000001
    > > $bin_2 = 1000101010101
    > > $result = 1000101000001

    >
    > In addition to the other answers you got, you can do this as strings as
    > well:
    >
    > print "1011101000001" & "1000101010101";
    >
    > The good thing about this is that it is not limited to 32 or 64 bits.
    > The bad thing is that if the strings aren't the same length they will
    > be aligned in an unintuitive (for numbers) manner.
    >
    > And or course the answer is a string rather than a number. Whether that is
    > good or bad depends on what you want.
    >
    > Xho
    >
    > --
    > --------------------http://NewsReader.Com/--------------------
    > The costs of publication of this article were defrayed in part by the
    > payment of page charges. This article must therefore be hereby marked
    > advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    > this fact.


    Thanks all for tips ..got it now. You guys are best
    /Cheers
    , Nov 16, 2007
    #4
    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,766
    darshana
    Oct 26, 2008
  2. Replies:
    12
    Views:
    2,654
    Slartibartfast
    Sep 23, 2003
  3. Replies:
    4
    Views:
    389
    Ronald Bruck
    Jul 22, 2006
  4. Replies:
    17
    Views:
    14,193
    shahzadafzal
    Dec 17, 2008
  5. hara

    join two binary numbers

    hara, May 22, 2006, in forum: Perl Misc
    Replies:
    11
    Views:
    330
    David Squire
    May 23, 2006
Loading...

Share This Page