Re: join two binary numbers



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
.



Relevant Pages

  • Re: [opensuse] Quick perl question - why are @array[$num] and $array[$num] the same?
    ... I don't know who I have to thank for this, but I think it was Anders who recommended that I look at perl after one of my last weird bash questions. ... my $NUM = 1; ... open FILE, ".bashrc" or die $!; ... The "use warnings;" gives you the message above. ...
    (SuSE)
  • Re: new commands written in perl
    ... > use warnings; on the other hand, ... > my $num; ... [dformosa@dformosa perl]$ perl -v ... GNU General Public License, which may be found in the Perl 5 source kit. ...
    (comp.lang.perl.misc)
  • How to make a prog more efieiant (in C)
    ... int get_summ_digits(int num); ... if (dig> res) ... while (num / multiplier) ...
    (Debian-User)
  • Re: Integer data type
    ... print $num; ... Why $num is bigger than LONG_MAX? ... Perl doesn't have an int and string and char ... Perl has a scalar and an array and a hash. ...
    (perl.beginners)
  • Re: how are these two strings not equal?
    ... perl 5.10 on Windows 2003 Server, ... TextPad and then run "compare files", ... return $res; ... Maybe some spaces are tabs or vice ...
    (perl.beginners)