Velocity Reviews > Perl > How to add two binary numbers using bitwise AND

# How to add two binary numbers using bitwise AND

cyrusgreats@gmail.com
Guest
Posts: n/a

 11-15-2007
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.

Joost Diepenmaat
Guest
Posts: n/a

 11-16-2007
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.

Look up "bitwise and" in perlop.

Joost.

xhoster@gmail.com
Guest
Posts: n/a

 11-16-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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 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
this fact.

cyrusgreats@gmail.com
Guest
Posts: n/a

 11-16-2007
On Nov 16, 8:47 am, (E-Mail Removed) wrote:
> (E-Mail Removed) 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