On May 4, Sasha Pachev wrote:
> Regarding the primitives, I agree that a skillful scripting language
> programmer can cut down the number of primitives for a good number of
> problems, but at the same time I can think of a string operation that
> would require a lot of primitives in a scripting language to perform.
> E.g - what if you needed to something crazy like this:
>
> for each byte:
> rotate the bits
> swap bits 1 and 7
> and bits 4 and 5 with some bitmask
> or bits 4 and 3 with another bitmask
> etc
>
> 20 really odd bit operations
>
> For something like this, even if the development time was an issue and
> not just the performance, I would choose see just because I can do the
> bit magic without having to look anything up, while in Perl or Python
> - my scripting languages of choice - I could not do it without having
> to use a reference.
>
> BTW, how many of us could? - if you can rotate the bits (let's be
> specific and say down) of each byte in a string in Perl or Python
> without having to look anything up, go ahead and post.

The bit operations are the easy part; they're the same in Perl and
Python as in C and C++.  The only part that would need to be looked up,
if you haven't used the language in quite some time, is getting the
integer value of each character from the input.  Python 2:

def bitwise_example(s):
    for c in s:
        if not len(c):
            break
        n = ord(c)
        n = (n >> 1) | ((n&1) << 7)     # Rotate the bits.
        n &= 0b00110000 & AND_MASK      # AND bits 4 and 5.
        n |= 0b00011000 & OR_MASK       # OR bits 4 and 3.
        c = chr(n)
        yield c

def main():
    from sys import argv, stdin, stdout
    inputs = [open(name, "rb") for name in argv[1:]] or [stdin]
    for f in inputs:
        for c in bitwise_example(f.read()):
            stdout.write(c)

if __name__ == "__main__":
    main()

Untested, mostly because AND_MASK and OR_MASK are undefined.  Python 3
would require some changes, perhaps limited to removing the ord() and
chr() calls.  One could also define a mutable string type to make the
return path more efficient, but there's not really much point.

Granted, I can understand having to use a reference for Perl.  I was
nearly fluent half my life ago, but Python is just so much more
convenient that I've let my Perl skills rust to the point that even
simple tasks require a few trips to the man pages.

For the same reason, I would avoid C for this kind of task unless and
until it was benchmarked as a significant bottleneck.  Looking up the
input and output routines would suck more time than compiling, and
compiling would suck more time than writing the bitwise handling, and
then I would want to scour the code for an inevitable memory leak.

- Eric

/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/

Reply via email to