How to add two binary numbers using bitwise AND
How to add two binary numbers using bitwise AND
am 16.11.2007 00:45:28 von cyrusgreats
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..
Re: How to add two binary numbers using bitwise AND
am 16.11.2007 01:56:33 von Joost Diepenmaat
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.
Re: How to add two binary numbers using bitwise AND
am 16.11.2007 02:15:59 von Jim Gibson
In article
,
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..
Use the 'and' ('&') operator:
#!/usr/local/bin/perl
use warnings;
use strict;
my $bin_1 = 0b1011101000001;
my $bin_2 = 0b1000101010101;
my $result = ($bin_1 & $bin_2);
printf " bin_1: %13b (%d)\n bin_2: %13b (%d)\nresult: %13b (%d)\n",
$bin_1, $bin_1, $bin_2, $bin_2, $result, $result;
__OUTPUT__
bin_1: 1011101000001 (5953)
bin_2: 1000101010101 (4437)
result: 1000101000001 (4417)
See 'perldoc perlop' and search for "Bitwise And" and "Bitwise String
Operators".
--
Jim Gibson
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Re: How to add two binary numbers using bitwise AND
am 16.11.2007 14:29:48 von hymie_
In our last episode, the evil Dr. Lacto had captured our hero,
cyrusgreats@gmail.com, who said:
>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.
This is the second question I've read in two days involving extraneous
changing of number bases. Did I miss a memo?
It's just as easy to say
5 & 3 = 1
as it is to say
0101 & 0011 = 0001
Second, if I remember correctly, the use of bases in perl is almost
entirely limited to input and output. perl has no particular need
to take a decimal number, explicitly convert it into binary, and
then explicity convert it into hex.
--hymie! http://lactose.homelinux.net/~hymie hymie@lactose.homelinux.net
------------------------ Without caffeine for 381 days ------------------------
Re: How to add two binary numbers using bitwise AND
am 16.11.2007 17:47:52 von xhoster
cyrusgreats@gmail.com 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.
Re: How to add two binary numbers using bitwise AND
am 16.11.2007 18:15:31 von cyrusgreats
On Nov 16, 8:47 am, xhos...@gmail.com wrote:
> cyrusgre...@gmail.com 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