want to join two binary numbers

H

hara

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

David Squire

hara said:
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
 
A

Allen Egerton

hara said:
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.
 
M

Matt Garrish

Allen Egerton said:
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
 
D

David Squire

David said:
hara said:
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;

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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top