home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / perl / 4849 < prev    next >
Encoding:
Internet Message Format  |  1992-07-21  |  1.4 KB

  1. Path: sparky!uunet!wupost!usc!isi.edu!sondeen
  2. From: sondeen@isi.edu (Jeff Sondeen)
  3. Newsgroups: comp.lang.perl
  4. Subject: *Another* embarrassingly simple question
  5. Message-ID: <21982@venera.isi.edu>
  6. Date: 21 Jul 92 16:03:56 GMT
  7. Sender: news@isi.edu
  8. Organization: USC-Information Sciences Institute
  9. Lines: 53
  10.  
  11. How can I print a number as binary?  What I want is
  12.     $num = 5;
  13.     print "%4b", $num;    # but it doesn't give me 0101
  14.         ^^^
  15.         wishfull thinking
  16.  
  17.  
  18. By the way, I found some type of problem tring to use vec's.
  19.  
  20.     #! /local/bin/perl
  21.     
  22.     $num = 5;
  23.     for $bits (1..20) {
  24.         vec($numv, 0, $bits) = $num;
  25.         @num = reverse(split(//,unpack("b$bits", $numv)));
  26.         $line = join('',@num);
  27.         print "$line\n";
  28.     }
  29.  
  30. gives:
  31.  
  32. 1
  33. 01
  34. 101
  35. 0101
  36. 00101
  37. 000101
  38. 0000101
  39. 00000101
  40. 000000101
  41. 0000000101
  42. 00000000101
  43. 000000000101
  44. 0000000000101
  45. 00000000000101
  46. 000000000000101
  47. 0000010100000000    ## what happend here????
  48. 00000010100000000
  49. 000000010100000000
  50. 0000000010100000000
  51. 00000000010100000000
  52.  
  53. This is perl, version 4.0
  54.  
  55. $RCSfile: perl.c,v $$Revision: 4.0.1.6 $$Date: 91/11/11 16:38:45 $
  56. Patch level: 19
  57.  
  58. Any help would be appreciated on either problem.  Thanks,
  59. --
  60. /jeff.  "A sloppy, ungroomed appearance or inappropriate dress may be of
  61. great diagnostic significance, suggesting intellectual subnormality, a
  62. serious psychiatric disturbance, or a psychopathic disregard for
  63. social conventions."  -- Robert Gregory, Adult Intellectual Assessment
  64.