Loading ...
Sorry, an error occurred while loading the content.

RE: [PBML] two questions

Expand Messages
  • Bob Showalter
    ... With the + operator ... You have to do the conversion of these strings, AFAIK. Without a module, you can do something like: printf( %08b , 0b01100010 &
    Message 1 of 1 , Jun 3, 2004
    • 0 Attachment
      nanambalasi wrote:
      > I got two questions, first is there a way to add binary numbers?

      With the + operator

      > I mean we got two numbers
      > 01100010 and
      > 11001001
      > how do we OR, XOR and ADD these binaries. Without converting it to
      > decimal then doing & operation and then back to binary.

      You have to do the conversion of these strings, AFAIK. Without a module, you
      can do something like:

      printf("%08b", 0b01100010 & 0b11001001)

      which will print the string 01000000. There might be a module somewhere on
      CPAN that will let you manipulate those strings directly. Have you looked?

      > I know the longer way with a loop. Well, i know all the longer ways,
      > but no shorter :)
      >
      > Second question, I want to measure time taken for some operation, i
      > have
      > always used this method:
      > ($sec, $min, $hour)/caltime();

      I don't know what you're doing here.

      >
      > operation and other code goes here
      >
      > ($end_sec, $end_min, $end_hour)/caltime();
      >
      > if ($end_sec >$sec){
      > $dsNnd_sec-$sec;
      > }else{
      > $dsNnd_sec+60-$sec;
      > $end_min--;
      > }
      > if ($end_min >$min){
      > $dmNnd_min-$min;
      > }else{
      > $dmNnd_min+60-$min;
      > $end_hour--;
      > }
      > if ($end_hour >$hour){
      > $dhNnd_hour-$hour;
      > }else{
      > $dhNnd_hour+24-$hour;
      > }
      >
      > prent ("Time taken: $dh:$dm:$ds");
      >
      > But I know there must be a shorter way, please help. Thanks in advance

      my $t = time;
      do_something();
      $t = time - $t; # $t is now elapsed seconds

      printf("%dh:%dm:%ds", $t / 3600, ($t / 3600) % 60, $t % 60);

      See also Time::HiRes for sub-second timing.
    Your message has been successfully submitted and would be delivered to recipients shortly.