join two binary numbers

  • Permalink
  • submit to reddit
  • Email
  • Follow


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
Reply hara.acharya (48) 5/22/2006 11:31:32 AM

See related articles to this posting


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
Reply 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
Reply 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
Reply 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
Reply 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
Reply 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
Reply 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
Reply 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
Reply 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
Reply 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
Reply 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
Reply David 5/23/2006 10:42:58 AM
comp.lang.perl.misc 32763 articles. 7 followers. Post

11 Replies
118 Views

Similar Articles

[PageSpeed] 27


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

want to join two binary numbers
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? hara wrote: > 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 reg...

Algorithm to compare two binary numbers
Hi All: I'm looking for an extremely fast algorithm that will compare 2 integers, and return the number of bit differences between them. Specifically, 217 = 11011010 153 = 10011010 (only 1 bit differs, even though the numerical difference is large) 127 = 01111111 128 = 10000000 (7 bits differ, even though the numerical difference is small) Examples: f(217, 153) = 1 bit f(127, 128) = 7 bits f(255, 0) = 8 bits Thanks. * No Such Luck: > > I'm looking for an extremely fast algorithm that will compare 2 > integers, and return the number of bit differences between them. >...

How to add two binary numbers using bitwise AND
I like to know how to add two binaary numbers using bitwise AND something like that: $bin_1 = 1011101000001 $bin_2 = 1000101010101 $result = 1000101000001 basically user enter two decimal numbers and code change those numbers to binary then using bitwise prints the value/result the convert the value to hex numbers. Thanks in advance.. On Thu, 15 Nov 2007 15:45:28 -0800, cyrusgreats wrote: > I like to know how to add two binaary numbers using bitwise AND > something like that: > $bin_1 = 1011101000001 > $bin_2 = 1000101010101 > $result = 1000101000001 That's not a...

Numbering in Access between two numbers
I have a question that I'm hoping someone here can answer. Let's say I have two fields which have a beginning number and an ending number. What I'd like to do is have Access generate a list of numbers between the beginning number and the ending number (inclusive) for each row that exists in my start table. For example, my start table might look like: Begin End 513525 513528 513530 513538 I would want my end table to look like: NPA-NXX 513525 513526 513527 513528 513530 513531 513532 513533 513534 513535 513536 513537 513538 We are trying to build a list of NPA-NXXs ...

(8-bit binary to two digit bcd) or (8-bit binary to two digit seven segment)
how do i write a code for (8-bit binary to two digit bcd) or (8-bit binary to two digit seven segment)?? thanks. "Fangs" <super_fangs@hotmail.com> wrote in message news:8997c11eccbf1ba0d808d3d8d9ea1a8c@localhost.talkaboutprogramming.com... > how do i write a code for (8-bit binary to two digit bcd) or (8-bit binary > to two digit seven segment)?? The structural way to do this is to convert the 8 bit binary to BCD fist, then convert BCD to 7 segment. The BCD to 7 segment is very easy to implement. It is just a ROM with BCD as the input and seven segment as the output....

How to generate random number between two numbers
I want to generate a randowm number between two numbers x and y i.e int randomNoBetween(int x, int y); Plz help Is there any such function?? Sanchit wrote: > I want to generate a randowm number between two numbers x and y i.e > > int randomNoBetween(int x, int y); The (I guess) simplest way to get a random number in the interval [0, n) (that is, 0 inclusive, n exclusive) is (if n is small enough) probably (rand() % n), which will however slightly favor smaller numbers. I can't tell if there's any "consensus" on whether to use this method or some more expensi...

joining two files paste or join
Hello, I have to files (csv) that I need to make into 1 file, but I am having issues with paste. Here is what I have, file1, DE,011,DAILY ,, VALID ,, OVER file2, NQ,PS,CZ ,,EM ,,HU I need to end up with the file like so: DE,011,DAILY,NQ,PS,CZ ,, VALID,,EM ,, OVER,,HU I have messed around with paste using different delimiters, but cannot get the desired result. Was hoping if someone could point me in the right direction on this one. Thanks Pete. On 2009-06-12, peter sands <peter_sands@techemail.com> wrote: > Hello, > I have to files (csv) that I need to make into 1 file,...

How do you convert a hexadecimal number to a binary number?
I can't find a clue how to convert a hexadecimal number to a binary number. Anyone know? -- Posted via http://www.ruby-forum.com/. [Note: parts of this message were removed to make it a legal post.] number.to_s(2) 2008/5/30, Bob Sanders <small.business.strategy@gmail.com>: > > I can't find a clue how to convert a hexadecimal number to a binary > number. Anyone know? > > -- > Posted via http://www.ruby-forum.com/. > > From: "Bob Sanders" <small.business.strategy@gmail.com> > >I can't find a clue how to convert a hexadecima...

the number 1 that is most right of binary number
Hello newsgroup, In some of my work , I am dealing with binary numbers. Dec Binary ----- ------------------- 1 1 0 0 0 2 0 1 0 0 3 1 1 0 0 4 0 0 1 0 5 1 0 1 0 6 0 1 1 0 7 1 1 1 0 8 0 0 0 1 9 1 0 0 1 10 0 1 0 1 After converting a decimal number to its binary form using the "de2bi" function, for each digit, I need to find the posit...

Convert decimal number in binary number
Hello, is there a function in vhdl to convert a decimal in to a BCD? Or have I to do this manual ? And if this is the only way, how can I do this? Martin makok wrote: > Hello, > > is there a function in vhdl to convert a decimal in to a BCD? Or have I to > do this manual ? And if this is the only way, how can I do this? This might work. I haven't looked at it, I just googled for 2 secs, but it looks like it's what you want. http://www.vhdl-online.de/model_lib_patras/vhdl_sources/special/special.htm#bin_bcdN -- Anders ...

converting binary numbers to real numbers
Please tell me how to convert binary numbers to real numbers. How to do the programme? Thank you. In article <ef0e4d4.-1@webx.raydaftYaTP>, "malar nayana" <mv_nayana@yahoo.com> wrote: > Please tell me how to convert binary numbers to real numbers. How to > do the programme? > > Thank you. ------------------------- What form are your binary numbers in? Strings, vectors of 1's and 0's, what? Do they represent integers, or if not what determines their binary points? (Remove "xyzzy" and ".invalid" to send me email.) Roger Staffor...

Joining two tables on two fields
Hello all, I am joining two tables in a query and the output should be all those records where two fields in table1 match two corresponding fields in table2. I joined the tables using both fields in design view and the Select statement in SQL view looks good. The query runs perfectly and shows the result I want but when I save the query and close it, re-opening in design view shows the error message "Microsoft Office Access can't represent the join expression {Table1].[Fieldx] = [Table2].[Fieldx] in design view." This is okay as long as it does not affect the query but when I r...

How to join two COUNT results from two tables
Hi I have an email campagin system while sending each mails i am recording it with current timestamp sample data number email tim_stmp 1 cxcx@yahoo.com 2008-12-25 13:34:38 2 fggg@yahoo.com 2008-12-26 13:37:30 3 ccx@yahoo.com 2008-12-26 13:34:38 4 abc@yahoo.com 2008-12-29 12:18:15 5 ddd@yahoo.com 2008-12-29 12:01:18 When i get responses i am tracking those too number eml_id cur_time 1 1 2008-12-27 20:45:10 2 3 2008-12-27 22:18:15 3 2 2008-12-29 18:14:18 4 4 2008-12-29 20:52:05 Now Am trying to make a report that prints a daily status from those tables and i need the results...

How do I get a random number between two random numbers?
I want to create a program that asks you to guess a number between two numbers. The problem is that I can't figure out how to make the numbers that you're guessing between random and have the random number you are guessing between those two numbers. Does anybody know how to do this? -- Posted via http://www.ruby-forum.com/. Alex Untitled wrote: > I want to create a program that asks you to guess a number between two > numbers. The problem is that I can't figure out how to make the numbers > that you're guessing between random and have the random number you are > ...

joining two tables, but then comparing the join fields with NOT...
I have two tables... One that lists the loan officer's information and one that lists all the people who have statement of accountabilites filed for them. Right now I have them joined on the loan officers name. Problem is, I need to select all the loan officers from the loan officer table who are not listed in the statement of accountability table. The current query I have for this is: SELECT LoanOfficers.Name, LoanOfficers.HireDate, LoanOfficers.Inactive, LoanOfficers.[SS#], LoanOfficers.Payment, LoanOfficers.State FROM LoanOfficers WHERE (((LoanOfficers.Name)<>[StatementOfAccounta...

Generating unique number from two other unique numbers
I would like to generate a unique number from two other unique numbers. With the two input numbers, one is a customer ID number (e.g. 23 = "John Kerry", 97 = "George Bush" etc.) and the other number is a promotion ID (e.g. 5 = "October two-for-one sale", 8 = "Summer free shipping promotion" etc.) The two input numbers are NOT unique against themselves i.e. I could have a customer # 23 redeeming promotion # 23. They are only unique amongst themselves. The end goal is that from a generated number I am able to decode back the customer ID and promotion ID...

how two join and arrange two files together
Hi, I have two files having entries like:-- fileA 8 ALA H = 7.85 N = 123.95 CA = 54.67 HA = 2.98 C = 179.39 15 ALA H = 8.05 N = 119.31 CA = 52.18 HA = 4.52 C = 177.18 23 ALA H = 8.78 N = 120.16 CA = 55.84 HA = 4.14 C = 179.93 and fileB ChainA: ALA8 -67.217297 -37.131330 ChainA: ALA21 -69.822977 -48.871282 ChainA: ALA23 -59.148095 -46.540043 ChainA: ALA33 -65.459303 -43.269718 i want to join thses two files in such a way that the output file will contain column of both the files and the enties of similar position of ALA will be together.so the output file should look something like:---- fil...

join two tables with sharing some columns between two
Hi, I have two tables like these: (this is an example, the actual tables have diffferent fields and meanings) TABLE1 id person_name date_arrival date_departure TABLE2 id car_description date_arrival date_departure I'd like to make a query to have such resulting table RESULTING_TABLE id person_name car_description date_arrival date_departure the id is the primary key for the three tables and it's unique for the three (id in table1 cannot be in table2, I use the same counter to generate the id for table1 and table2). I is possible to create such a query? Thanks filippo ...

join two tables with sharing some columns between two
Hi, I have two tables like these: (this is an example, the actual tables have diffferent fields and meanings) TABLE1 id person_name date_arrival date_departure TABLE2 id car_description date_arrival date_departure I'd like to make a query to have such resulting table RESULTING_TABLE id person_name car_description date_arrival date_departure the id is the primary key for the three tables and it's unique for the three (id in table1 cannot be in table2, I use the same counter to generate the id for table1 and table2). I is possible to create such a q...

How can I convert a Hex number to a binary number?
Hi, I am using the following function for decimal numbers but I have a neeed to convert hex to binary. Public Function Binary(Number As Long) As String If Abs(Number) > 1 Then Binary = Binary(Number \ 2) & CStr(Abs(Number Mod 2)) Else Binary = CStr(Number) End If End Function thanks Kirk > I am using the following function for decimal numbers but I have a neeed to > convert hex to binary. > > Public Function Binary(Number As Long) As String > If Abs(Number) > 1 Then > Binary = Binary(Number \ 2) & CStr(Abs(N...

How to find the closest two numbers between two sets of data?
How to find the closest two numbers between two sets of data? Say two vectors A and B, how to find a (from A) and b (from B) so that abs(a-b) is the smallest among all? I could do this by a simple (stupid) thorough search, is there a better way in Matlab? Thanks, Jim Jim River wrote: > > > How to find the closest two numbers between two sets of data? Say > two > vectors A and B, how to find a (from A) and b (from B) so that > abs(a-b) is the smallest among all? > > I could do this by a simple (stupid) thorough search, is there a > better way in Matlab? &g...

binary number loop
All, A co-worker of mine is working on a program using a financial model that has some binary tree type logic (sorry - don't know much more about than that) where she will know how many "levels" she has - say 3 - and then needs to generate all binary numbers from 000 to 111 - and the process will do some calculations for each one. She was originally trying a multistage loop where she set the first digit, then looped through 0 and 1 to set the 2nd, etc. but it was very inefficient plus gets more and more complicated the greater the number of "levels" the tree will bran...

Counting number of numbers in a large list between two valus
Dear Listers, I have a large (5-20million) one dimensional list of real numbers and I want to count the number of entries in the list that lie between 2 specific values (x1, x2). I need to run the function for a number of different ranges. ie. number of list entries (l), where x1 <= l <= x2 I've tried: tallydata[{x1_, x2_}] := Count[data, x_ /; x1 <= x <= x2] that takes about 3-4 seconds and tallydata[{x1_, x2_}] := Length[Select[data, x1 <= # <= x2 &]] which takes a little bit longer. The best I've managed is (this last one might be ...

Enumerating all possible binary numbers from given number of columns
Hi, does anyone have code that will enumerate all of the binary numbers possible from a user specified number of columns? e.g. if No of Columns = 2 Output = 00 10 01 11 any help appreciated L La di da Limey wrote: > > > Hi, > > does anyone have code that will enumerate all of the binary numbers > possible > from a user specified number of columns? > > e.g. if No of Columns = 2 > > Output = > 00 > 10 > 01 > 11 > > any help appreciated > limeys need help with simple homework? you can figure out how to get the input to the formula: ...