want to join two binary numbers

Discussion in 'Perl Misc' started by hara, May 22, 2006.

  1. hara

    hara Guest

    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, May 22, 2006
    #1
    1. Advertising

  2. hara

    David Squire Guest

    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
    regularly. In particular, use a small, complete script to illustrate
    your questions/problems.

    What you say above again illustrates your failure to address the point I
    raised earlier in response to your earlier post: you need to decided
    whether you are dealing with numbers or strings. You seem to want
    numbers, but are using strings.

    Here's how to do it with numbers:

    ----

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

    my $a = 0b11;
    my $res = 0b0000 | $a;
    printf "%b\n", $res;

    ----

    Output:

    11

    DS
     
    David Squire, May 22, 2006
    #2
    1. Advertising

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




    use strict;
    use warnings;

    $a=11;
    $res="0000" | "$a" ;

    Global symbol "$res" requires explicit package name at test.pl line 5.
    Execution of test.pl aborted due to compilation errors.

    --
    Allen Egerton
    aegerton at pobox dot com
     
    Allen Egerton, May 22, 2006
    #3
  4. hara

    Matt Garrish Guest

    "Allen Egerton" <> wrote in message
    news:...
    > 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?
    >>

    >
    > use strict;
    > use warnings;
    >
    > $a=11;
    > $res="0000" | "$a" ;
    >
    > Global symbol "$res" requires explicit package name at test.pl line 5.
    > Execution of test.pl aborted due to compilation errors.
    >


    Is there a point to this, or do you really not know how to declare
    variables?

    If you don't know why $a isn't causing the error see perlvar.

    Matt
     
    Matt Garrish, May 22, 2006
    #4
  5. hara

    Dr.Ruud Guest

    hara schreef:

    > Suppose $a=11
    > I want to make it 0011


    $ perl -e '
    $x = "11";
    $y = sprintf "%04b", oct "0b$x";
    print "$y\n"
    '
    0011

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, May 22, 2006
    #5
  6. hara

    David Squire Guest

    David Squire wrote:
    > 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?


    [snip]

    > Here's how to do it with numbers:
    >
    > ----
    >
    > #!/usr/bin/perl
    > use strict;
    > use warnings;
    >
    > my $a = 0b11;
    > my $res = 0b0000 | $a;
    > printf "%b\n", $res;
    >
    > ----
    >
    > Output:
    >


    Or, just for fun, you could do it this way, with strings:

    ----

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

    my $a = '3';
    my $res = '0' | $a;
    printf "%04b\n", $res;

    ----

    Output: 0011
     
    David Squire, May 22, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fangs
    Replies:
    3
    Views:
    9,802
    darshana
    Oct 26, 2008
  2. Alan Silver
    Replies:
    0
    Views:
    890
    Alan Silver
    Jun 5, 2006
  3. googleboy
    Replies:
    1
    Views:
    929
    Benji York
    Oct 1, 2005
  4. hara

    join two binary numbers

    hara, May 22, 2006, in forum: Perl Misc
    Replies:
    11
    Views:
    341
    David Squire
    May 23, 2006
  5. Replies:
    3
    Views:
    430
Loading...

Share This Page