Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > want to join two binary numbers

Reply
Thread Tools

want to join two binary numbers

 
 
hara
Guest
Posts: n/a
 
      05-22-2006
Suppose
$a=11
I want to make it 0011
If i will do like
$res="0000" | "$a" ;
this will come as 1100
But i want to make it 0011
How can i do this?

 
Reply With Quote
 
 
 
 
David Squire
Guest
Posts: n/a
 
      05-22-2006
hara wrote:
> Suppose
> $a=11
> I want to make it 0011
> If i will do like
> $res="0000" | "$a" ;
> this will come as 1100
> But i want to make it 0011
> How can i do this?
>


You have already asked a very similar question in another thread here.
Please don't start multiple threads for your question.

Please read the posting guidelines for this group, which are posted here
regularly. In particular, use a small, complete script to illustrate
your questions/problems.

What you say above again illustrates your failure to address the point I
raised earlier in response to your earlier post: you need to decided
whether you are dealing with numbers or strings. You seem to want
numbers, but are using strings.

Here's how to do it with numbers:

----

#!/usr/bin/perl
use strict;
use warnings;

my $a = 0b11;
my $res = 0b0000 | $a;
printf "%b\n", $res;

----

Output:

11

DS
 
Reply With Quote
 
 
 
 
Allen Egerton
Guest
Posts: n/a
 
      05-22-2006
hara wrote:
> Suppose
> $a=11
> I want to make it 0011
> If i will do like
> $res="0000" | "$a" ;
> this will come as 1100
> But i want to make it 0011
> How can i do this?
>




use strict;
use warnings;

$a=11;
$res="0000" | "$a" ;

Global symbol "$res" requires explicit package name at test.pl line 5.
Execution of test.pl aborted due to compilation errors.

--
Allen Egerton
aegerton at pobox dot com
 
Reply With Quote
 
Matt Garrish
Guest
Posts: n/a
 
      05-22-2006

"Allen Egerton" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> hara wrote:
>> Suppose
>> $a=11
>> I want to make it 0011
>> If i will do like
>> $res="0000" | "$a" ;
>> this will come as 1100
>> But i want to make it 0011
>> How can i do this?
>>

>
> use strict;
> use warnings;
>
> $a=11;
> $res="0000" | "$a" ;
>
> Global symbol "$res" requires explicit package name at test.pl line 5.
> Execution of test.pl aborted due to compilation errors.
>


Is there a point to this, or do you really not know how to declare
variables?

If you don't know why $a isn't causing the error see perlvar.

Matt


 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      05-22-2006
hara schreef:

> Suppose $a=11
> I want to make it 0011


$ perl -e '
$x = "11";
$y = sprintf "%04b", oct "0b$x";
print "$y\n"
'
0011

--
Affijn, Ruud

"Gewoon is een tijger."


 
Reply With Quote
 
David Squire
Guest
Posts: n/a
 
      05-22-2006
David Squire wrote:
> hara wrote:
>> Suppose
>> $a=11
>> I want to make it 0011
>> If i will do like
>> $res="0000" | "$a" ;
>> this will come as 1100
>> But i want to make it 0011
>> How can i do this?


[snip]

> Here's how to do it with numbers:
>
> ----
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my $a = 0b11;
> my $res = 0b0000 | $a;
> printf "%b\n", $res;
>
> ----
>
> Output:
>


Or, just for fun, you could do it this way, with strings:

----

#!/usr/bin/perl
use strict;
use warnings;

my $a = '3';
my $res = '0' | $a;
printf "%04b\n", $res;

----

Output: 0011
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(8-bit binary to two digit bcd) or (8-bit binary to two digit seven segment) Fangs VHDL 3 10-26-2008 06:41 AM
How to add two binary numbers using bitwise AND cyrusgreats@gmail.com Perl Misc 3 11-16-2007 05:15 PM
You must join New York Paid To Read !!!!!!! NewYork PTR is online!!! JOIN NOW!!!!!! Alan Silver ASP .Net 0 06-05-2006 03:27 PM
join two binary numbers hara Perl Misc 11 05-23-2006 10:42 AM
list.join()... re.join()...? Do they exist? (newbie questions...) googleboy Python 1 10-01-2005 12:56 PM



Advertisments