f



join two binary numbers

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

0
5/22/2006 11:31:32 AM
comp.lang.perl.misc 33226 articles. 1 followers. brian (1246) is leader. Post Follow

11 Replies
402 Views

Similar Articles

[PageSpeed] 56

hara wrote:
> 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
0
David
5/22/2006 11:37:01 AM
hara wrote:
> 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;


-- 
Tony Green
Ipswich, Suffolk, UK, http://www.beermad.org.uk
* This has been a Microsoft-free message *
0
Tony
5/22/2006 11:37:41 AM
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
0
Mirco
5/22/2006 11:40:58 AM
hara wrote:
> 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
-- 
use Perl;
program
fulfillment
0
John
5/22/2006 12:02:54 PM
hara <hara.acharya@gmail.com> wrote:
> 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.


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas
0
Tad
5/22/2006 12:54:18 PM
>>>>> "TG" == Tony Green <news@beermad.REMOVE.TO.REPLY.org.uk> writes:

  TG> hara wrote:
  >> 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
  >> 
  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

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org
0
Uri
5/22/2006 10:32:08 PM
"hara" <hara.acharya@gmail.com> wrote:

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


-- 
John Bokma          Freelance software developer
                                &
                    Experienced Perl programmer: http://castleamber.com/
0
John
5/22/2006 11:27:13 PM
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") ;

-- 
Affijn, Ruud

"Gewoon is een tijger."


0
Dr
5/23/2006 6:43:56 AM
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?

0
hara
5/23/2006 8:04:13 AM
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



0
Mirco
5/23/2006 8:36:09 AM
hara wrote:
> 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.

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
0
David
5/23/2006 10:42:58 AM
Reply: