home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!wupost!usc!isi.edu!sondeen
- From: sondeen@isi.edu (Jeff Sondeen)
- Newsgroups: comp.lang.perl
- Subject: *Another* embarrassingly simple question
- Message-ID: <21982@venera.isi.edu>
- Date: 21 Jul 92 16:03:56 GMT
- Sender: news@isi.edu
- Organization: USC-Information Sciences Institute
- Lines: 53
-
- How can I print a number as binary? What I want is
- $num = 5;
- print "%4b", $num; # but it doesn't give me 0101
- ^^^
- wishfull thinking
-
-
- By the way, I found some type of problem tring to use vec's.
-
- #! /local/bin/perl
-
- $num = 5;
- for $bits (1..20) {
- vec($numv, 0, $bits) = $num;
- @num = reverse(split(//,unpack("b$bits", $numv)));
- $line = join('',@num);
- print "$line\n";
- }
-
- gives:
-
- 1
- 01
- 101
- 0101
- 00101
- 000101
- 0000101
- 00000101
- 000000101
- 0000000101
- 00000000101
- 000000000101
- 0000000000101
- 00000000000101
- 000000000000101
- 0000010100000000 ## what happend here????
- 00000010100000000
- 000000010100000000
- 0000000010100000000
- 00000000010100000000
-
- This is perl, version 4.0
-
- $RCSfile: perl.c,v $$Revision: 4.0.1.6 $$Date: 91/11/11 16:38:45 $
- Patch level: 19
-
- Any help would be appreciated on either problem. Thanks,
- --
- /jeff. "A sloppy, ungroomed appearance or inappropriate dress may be of
- great diagnostic significance, suggesting intellectual subnormality, a
- serious psychiatric disturbance, or a psychopathic disregard for
- social conventions." -- Robert Gregory, Adult Intellectual Assessment
-