join two binary numbers

H

hara

I want to join two binary numbers like
$a=1101
$b=1001
and i want the result to be 11011001
join is not working for me.
Can anybody suggest something
 
D

David Squire

hara said:
I want to join two binary numbers like
$a=1101
$b=1001
and i want the result to be 11011001
join is not working for me.
Can anybody suggest something

Do you want them to be treated as numbers or strings? Please post a
complete example Perl script (the lines above would not compile).

DS
 
T

Tony Green

hara said:
I want to join two binary numbers like
$a=1101
$b=1001
and i want the result to be 11011001
join is not working for me.
Can anybody suggest something
How about:
$c = $a . $b;
 
M

Mirco Wahab

Hi hara
I want to join two binary numbers like
$a=1101
$b=1001
and i want the result to be 11011001
join is not working for me.

as David already pointed out, your
example does not provide information
on what you *really* want.
Can anybody suggest something

$a = '1101';
$b = '1001';

$c = $a . $b;
print $c;


Regards,

Mirco
 
J

John W. Krahn

hara said:
I want to join two binary numbers like
$a=1101
$b=1001
and i want the result to be 11011001
join is not working for me.
Can anybody suggest something

$ perl -e'
my $x = 0b1101;
my $y = 0b1001;
printf "%b\n", $x << 4 | $y;
'
11011001


John
 
T

Tad McClellan

hara said:
I want to join two binary numbers like
$a=1101
$b=1001


Those are in decimal, not in binary.

"join" is not an operation that is defined for numbers.

and i want the result to be 11011001


You do not have 2 binary numbers then, you have 2 strings.

The operation defined for "joining" strings is named "concatenation".

join is not working for me.


If you show us your broken code, we can help you fix it.

Can anybody suggest something


Show the code you have so far.
 
U

Uri Guttman

TG> How about:
TG> $c = $a . $b;

if join didn't do the trick, why would . be any better? join is just a
loop over .

and this is beside the point that the OP's question is massively vague
as we have no clue as to the format of the data and desired results.

uri
 
J

John Bokma

hara said:
I want to join two binary numbers like
$a=1101
$b=1001

Notice that a and b are not binary numbers, but 1,101 and 1,001.
and i want the result to be 11011001
join is not working for me.

try:

printf "%04d%04d", $a, $b;

Since you didn't give a clear example of "not working" I had to guess, and
my guess is that your leading zeroes are eaten away:

$a = 0010;
$b = 1001;

Another option is to quote your numbers, so they are strings, since you
are glueing them together as strings. E.g.

$a = '0010';
$b = '1001';

print "$a$b";


Final note: don't use $a and $b, it might bite you one day.
 
D

Dr.Ruud

hara schreef:
I want to join two binary numbers like
$a = 1101
$b = 1001
and i want the result to be 11011001
join is not working for me.
Can anybody suggest something

my $x = '1101' ;
my $y = '1001' ;

printf "%08b\n", oct("0b$x") << 4 | oct("0b$y") ;
 
H

hara

My idea is to make a binary number into a 6 digit number.
From a calculation i may get a 2/3/4 digit number and i want to convert
it to 6 digit number by adding zeros on the left of that number.
Suppose i got
$a="1101";
To make it 6 digit i tried doing it like
$res="000000" | $a;
But i am getting
110100
But i want
001101
Is it possible to do that?
 
M

Mirco Wahab

Hi hara
Suppose i got
$a="1101";
To make it 6 digit i tried doing it like
$res="000000" | $a;
But i am getting
110100
But i want
001101
Is it possible to do that?

You can do all these things with printf/sprintf:

$number = 33; # decimal
...
printf "%06b", $number;
...

or you can use some ideas from the
Perl-Cookbook (Ch. 2.4)

...
$n = bin2dec('0110110');
printf "%06b", $n;
...
sub bin2dec {
return unpack("N", pack("B32", substr("0" x 32 . shift, -32)));
}
...


But you should'n be confused about
the 'internal' representation of
numbers.

Regards

Mirco
 
D

David Squire

hara said:
My idea is to make a binary number into a 6 digit number.
it to 6 digit number by adding zeros on the left of that number.

Finally you tell us what you really want: it is an output formatting
problem. Nothing to do with or'ing binary numbers. Do you really imagine
that Perl is somehow storing binary numbers of sub-byte size?

You are still ignoring the posting guidelines for this group though.
Many here will be running out of patience. Quote context. Post complete
scripts.
Suppose i got
$a="1101";
To make it 6 digit i tried doing it like
$res="000000" | $a;
But i am getting
110100

As you have been told many times already, that is because "1101" is a
*string* of four characters, and "000000" is a *string* of six
characters. They are not binary numbers.

Let's consider your example. Complete script:

----

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

my $num = '1101';
my $res = '000000' | $num;
print $res;

----

Output: 110100

What is happening here? Perl is doing an bitwise or of each *byte* of
the two strings, padding $num of the right with two zero bytes to match
the length of $res.

Perhaps this will convince you. The ASCII code for '3' is 0b00110011.
For '4' it is 0b00110100. For '7' it is 0b00110111. So, a bitwise or of
'3' and '4' will give '7'. Let's see:

----

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

my $num = '1103';
my $res = '000400' | $num;
print $res;

----

Output: 110700

Likewise '1103' | '000a00' results in '110s00', since the ASCII codes
are '3': 0b00110011, 'a': 0b01100001, 's': 0b01110011.

But i want
001101
Is it possible to do that?

See the many solutions already posted here.

DS
 

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

Members online

Forum statistics

Threads
474,186
Messages
2,570,998
Members
47,587
Latest member
JohnetteTa

Latest Threads

Top