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

two questions

Expand Messages
  • nanambalasi
    I got two questions, first is there a way to add binary numbers? I mean we got two numbers 01100010 and 11001001 how do we OR, XOR and ADD these binaries.
    Message 1 of 2 , Jun 3, 2004
    • 0 Attachment
      I got two questions, first is there a way to add binary numbers?
      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.
      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();

      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

      Respectfully, OkIDaN
    • merlyn@stonehenge.com
      ... nanambalasi I got two questions, first is there a way to add binary numbers? nanambalasi I mean we got two numbers nanambalasi 01100010 and nanambalasi
      Message 2 of 2 , Jun 3, 2004
      • 0 Attachment
        >>>>> "nanambalasi" == nanambalasi <okidan@...> writes:

        nanambalasi> I got two questions, first is there a way to add binary numbers?
        nanambalasi> I mean we got two numbers
        nanambalasi> 01100010 and
        nanambalasi> 11001001
        nanambalasi> how do we OR, XOR and ADD these binaries. Without converting it to
        nanambalasi> decimal then doing & operation and then back to binary.

        I don't know why you'd need to convert it to decimal. The number
        format of 0b01100010 works just fine as literals. If you are reading
        the data instead, pack "N", unpack "B*", $value works pretty well to
        convert it into a number (NOT decimal!).

        Probably what you want is:

        printf "%b\n", 0b01100010 & 0b11001001;

        Similarly, "|" for "or", "^" for "xor", and "+" for add.

        nanambalasi> Second question, I want to measure time taken for some operation, i
        nanambalasi> have
        nanambalasi> always used this method:

        See the DateTime modules in the CPAN.

        --
        Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
        <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
        Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
        See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
      Your message has been successfully submitted and would be delivered to recipients shortly.