Re: join two binary numbers
- From: David Squire <David.Squire@xxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 23 May 2006 11:42:58 +0100
hara wrote:
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.From a calculation i may get a 2/3/4 digit number and i want to convert
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
.
- References:
- join two binary numbers
- From: hara
- Re: join two binary numbers
- From: John W. Krahn
- Re: join two binary numbers
- From: hara
- join two binary numbers
- Prev by Date: Re: Performing substitutions on every string in an array using map
- Next by Date: Simple MD5 Hashing - Please Help!!!
- Previous by thread: Re: join two binary numbers
- Next by thread: Re: join two binary numbers
- Index(es):
Relevant Pages
|
|