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.

, Nov 15, 2007

2. Joost DiepenmaatGuest

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

> 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

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

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