Forgive me if this isn't the first time someone has done something like this. I was unable to find such an instance. Hopefully you will be as amused as I was...
#!/usr/bin/perl -w use strict; my $count; my $bits = ~(0); while ($bits) { $bits >>= 1; $count++; } print "$count bit words are being used.\n";

Replies are listed 'Best First'.
Re: word size
by BrowserUk (Patriarch) on Sep 08, 2005 at 03:22 UTC

    You might find the information available from Config useful. Here's a list of keys with 'size' in the the name:

    use Config;; printf "$_ => %s", $Config{ $_ }||'n/a' for grep /size/i, keys %Config +;; charsize => 1 d_chsize => define doublesize => 8 fpossize => 8 gidsize => 4 i16size => 2 i32size => 4 i64size => 8 i8size => 1 intsize => 4 ivsize => 4 longdblsize => 10 longlongsize => 8 longsize => 4 lseeksize => 8 nvsize => 8 ptrsize => 4 shortsize => 2 sig_size => 27 sizesize => 4 sizetype => size_t socksizetype => int ssizetype => int u16size => 2 u32size => 4 u64size => 8 u8size => 1 uidsize => 4 uvsize => 4

    intsize & ivsize are probably the most relevant for your purpose.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
      Ahh...but how much fun would that be? This was, after all, meant to be a CUFP; something that originated in some assembly I was working on. How often do you see the bit operations put to use? :o)

      The problem is, that all those sizes are in bytes, not bits. I'm not sure how you can compute the size of something in bits using Config, as it doesn't tell the number of bits in characters. Maybe you could use something like ivsize/i32size*32.

        Hmm. Now I know there have been machines with 6-bit bytes, and (maybe) 9-bit bytes, in the dark distant past, but I'm not aware of any remotely modern cpu that doesn't use 8-bit bytes?


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
Re: word size
by ambrus (Abbot) on Sep 08, 2005 at 21:25 UTC

    There's also this:

    $bits = length(unpack("B*",pack("J")));
Re: word size
by chanio (Priest) on Sep 11, 2005 at 04:24 UTC
    perl -e' use strict; my $bina; my $count; my $bits = ~(64); while ($bits) { prbit($bits); $bits >>= 1; $count++; } sub prbit { my $bi=shift; $bina.=($bi%2)? "0":"1"; } $bina=reverse($bina); print "$bina\n$count bit words are being used.\n";' __OUTPUTS__ 00000000000000000000000001000000 32 bit words are being used.
    The Lords production is only eaten by the Lords...

    Wherever I lay my KNOPPIX disk, a new FREE LINUX nation could be established

      Ahh..at least someone is going to humor me! We find ourselves with a fun base_10 to base_2 conversion.
      GOT_VOTES:{($collin{votes} > 0) ? do{$chanio++;last} : goto GOT_VOTES}
      :o)