Bitwise operations in Scheme 
Author Message
 Bitwise operations in Scheme

Hello Scheme Experts,

I am fairly new to Scheme (and LISP). Therefore, please, forgive me some
ignorance.

I wonder how can I perform standard bitwise operations
like (in C/C++ sintax)
&  -- bitwise AND
|  -- bitwise OR
^  -- bitwise Exclusive OR

Thank you in advance for your help



Sat, 29 Jun 2002 03:00:00 GMT  
 Bitwise operations in Scheme

Quote:

> I wonder how can I perform standard bitwise operations
> like (in C/C++ sintax)
> &  -- bitwise AND
> |  -- bitwise OR
> ^  -- bitwise Exclusive OR

I'm not claiming that these are the most efficient ... :) But this is what I
use.

Mayer
---------------------------------------
(define ^bitwise-op
  (lambda (op-combine do-if-arg1=0 do-if-arg2=0)
    (letrec ((bitwise-op2
              (lambda (arg1 arg2)
                (cond ((zero? arg1) (do-if-arg1=0 arg2))
                      ((zero? arg2) (do-if-arg2=0 arg1))
                      (else (+ (op-combine
                                (remainder arg1 2)
                                (remainder arg2 2))
                               (* 2 (bitwise-op2
                                     (quotient arg1 2)
                                     (quotient arg2 2))))))))
             (bitwise-op-list
              (lambda (s)
                (if (null? (cdr s)) (car s)
                    (bitwise-op2 (car s) (bitwise-op-list (cdr s)))))))
      (lambda s
        (bitwise-op-list s)))))

(define bitwise-and
  (^bitwise-op
   (lambda (a b) (if (or (zero? a) (zero? b)) 0 1))
   (lambda (b) 0)
   (lambda (a) 0)))

(define bitwise-or
  (^bitwise-op
   (lambda (a b) (if (and (zero? a) (zero? b)) 0 1))
   (lambda (b) b)
   (lambda (a) a)))

(define bitwise-xor
  (^bitwise-op
   (lambda (a b)
     (if (or (and (zero? a) (not (zero? b)))
             (and (not (zero? a)) (zero? b))) 1 0))
   (lambda (b) b)
   (lambda (a) a)))

(define bitwise-not
  (letrec ((bit-not (lambda (a) (if (zero? a) 1 0)))
           (bitwise-not
            (lambda (a)
              (cond ((= a 1) 0)
                    (else (+ (bit-not (remainder a 2))
                             (* 2 (bitwise-not (quotient a 2)))))))))
    (lambda (a)
      (if (zero? a) 1
          (bitwise-not a)))))

(define bytewise-endian
  (letrec ((bytewise-endian
            (lambda (n total)
              (if (zero? n) total
                  (bytewise-endian
                   (quotient n 256)
                   (+ (remainder n 256)
                      (* total 256)))))))
    (lambda (n)
      (bytewise-endian n 0))))



Sat, 29 Jun 2002 03:00:00 GMT  
 Bitwise operations in Scheme
Well, your implementation is pretty lengthy. Most CPUs perform bitwise
operations as a single instruction and many programming languages have
bitweise operations defined as primitives.
It really surprises me that Scheme lacks this functionality yet allowing
radix '2 numbers (bit representation).

thanks,
Leo


    >> I wonder how can I perform standard bitwise operations
    >> like (in C/C++ sintax)
    >> &  -- bitwise AND
    >> |  -- bitwise OR
    >> ^  -- bitwise Exclusive OR

    MG> I'm not claiming that these are the most efficient ... :) But this is what I
    MG> use.

    MG> Mayer
    MG> ---------------------------------------
    MG> (define ^bitwise-op
    MG>   (lambda (op-combine do-if-arg1=0 do-if-arg2=0)
    MG>     (letrec ((bitwise-op2
    MG>            (lambda (arg1 arg2)
    MG>              (cond ((zero? arg1) (do-if-arg1=0 arg2))
    MG>                    ((zero? arg2) (do-if-arg2=0 arg1))
    MG>                    (else (+ (op-combine
    MG>                              (remainder arg1 2)
    MG>                              (remainder arg2 2))
    MG>                             (* 2 (bitwise-op2
    MG>                                   (quotient arg1 2)
    MG>                                   (quotient arg2 2))))))))
    MG>           (bitwise-op-list
    MG>            (lambda (s)
    MG>              (if (null? (cdr s)) (car s)
    MG>                  (bitwise-op2 (car s) (bitwise-op-list (cdr s)))))))
    MG>       (lambda s
    MG>      (bitwise-op-list s)))))

    MG> (define bitwise-and
    MG>   (^bitwise-op
    MG>    (lambda (a b) (if (or (zero? a) (zero? b)) 0 1))
    MG>    (lambda (b) 0)
    MG>    (lambda (a) 0)))

    MG> (define bitwise-or
    MG>   (^bitwise-op
    MG>    (lambda (a b) (if (and (zero? a) (zero? b)) 0 1))
    MG>    (lambda (b) b)
    MG>    (lambda (a) a)))

    MG> (define bitwise-xor
    MG>   (^bitwise-op
    MG>    (lambda (a b)
    MG>      (if (or (and (zero? a) (not (zero? b)))
    MG>           (and (not (zero? a)) (zero? b))) 1 0))
    MG>    (lambda (b) b)
    MG>    (lambda (a) a)))

    MG> (define bitwise-not
    MG>   (letrec ((bit-not (lambda (a) (if (zero? a) 1 0)))
    MG>         (bitwise-not
    MG>          (lambda (a)
    MG>            (cond ((= a 1) 0)
    MG>                  (else (+ (bit-not (remainder a 2))
    MG>                           (* 2 (bitwise-not (quotient a 2)))))))))
    MG>     (lambda (a)
    MG>       (if (zero? a) 1
    MG>        (bitwise-not a)))))

    MG> (define bytewise-endian
    MG>   (letrec ((bytewise-endian
    MG>          (lambda (n total)
    MG>            (if (zero? n) total
    MG>                (bytewise-endian
    MG>                 (quotient n 256)
    MG>                 (+ (remainder n 256)
    MG>                    (* total 256)))))))
    MG>     (lambda (n)
    MG>       (bytewise-endian n 0))))



Sat, 29 Jun 2002 03:00:00 GMT  
 Bitwise operations in Scheme
+---------------
| I wonder how can I perform standard bitwise operations
| like (in C/C++ sintax)
| &  -- bitwise AND
| |  -- bitwise OR
| ^  -- bitwise Exclusive OR
+---------------

Even though there's no standard for it, most implementations of Scheme
have the usual bitwise operations builtin, e.g., in MzScheme they're
called "bitwise-and", "bitwise-ior", "bitwise-xor", "bitwise-not", as
well as "arithmetic-shift". E.g.

        > (define foo #xffffaaaa55550000)
        > foo
        18446650247285637120
        > (number->string foo 16)
        "ffffaaaa55550000"
        > (number->string (bitwise-xor foo (arithmetic-shift foo 8)) 16)
        "ff005500ff00550000"
        >

-Rob

-----

Applied Networking              http://reality.sgi.com/rpw3/
Silicon Graphics, Inc.          Phone: 650-933-1673
1600 Amphitheatre Pkwy.         FAX: 650-933-0511
Mountain View, CA  94043        PP-ASEL-IA



Sun, 30 Jun 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Doing bitwise AND operation in shell scripts

2. Bitwise operations

3. Bitwise Logical Operations?

4. bitwise operations

5. bitwise operations (try #2)

6. Question: Bitwise operations in Oberon

7. Bitwise operations on X's and Z's

8. bitwise operation on 64 bits value, how?

9. bitwise operations in Ada95

10. Help: bitwise operations on a 56 bit word

11. Help: bitwise operations on a 56 bit word

12. Bitwise Operations

 

 
Powered by phpBB® Forum Software