Velocity Reviews > Perl > want to join two binary numbers

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

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

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

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

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

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

 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 OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Fangs VHDL 3 10-26-2008 06:41 AM cyrusgreats@gmail.com Perl Misc 3 11-16-2007 05:15 PM Alan Silver ASP .Net 0 06-05-2006 03:27 PM hara Perl Misc 11 05-23-2006 10:42 AM googleboy Python 1 10-01-2005 12:56 PM

Advertisments