Annotation of 43BSD/contrib/icon/src/lib/bitops.icn, revision 1.1.1.1

1.1       root        1: #      BITOPS(2)
                      2: #
                      3: #      Logical operations on strings of 0s and 1s
                      4: #
                      5: #      Ralph E. Griswold
                      6: #
                      7: #      Last modified 4/27/83
                      8: #
                      9: #
                     10: 
                     11: #  convert integer i to bit string
                     12: #
                     13: procedure bitstring(i)
                     14:    local b, h
                     15:    if i < 0 then {
                     16:       h := 1
                     17:       i := -(i + 1)
                     18:       }
                     19:    else h := 0
                     20:    b := ""
                     21:    while i ~= 0 do {
                     22:       b := abs(i % 2) || b
                     23:       i /:= 2
                     24:       }
                     25:    b := right(b,32,"0")
                     26:    if h = 1 then b := neg(b)
                     27:    return b
                     28: end
                     29: 
                     30: #  logical "and" of b1 and b2
                     31: #
                     32: procedure and(b1,b2)
                     33:    return map(bsum(b1,b2),"12","01")
                     34: end
                     35: 
                     36: 
                     37: #  arithmetic sum of b1 and b2 (used by other procedures)
                     38: #
                     39: procedure bsum(b1,b2)
                     40:    static segment
                     41:    initial segment := 10
                     42:    if *b1 > *b2 then b1 :=: b2
                     43:    b1 := right(b1,*b2,"0")
                     44:    if *b1 <= segment then return b1 + b2
                     45:    return b1[1+:segment] + b2[1+:segment] ||
                     46:       bsum(b1[segment + 1:0],b2[segment + 1:0])
                     47: end
                     48: 
                     49: #  convert bitstring b to integer
                     50: #
                     51: procedure decimal(b)
                     52:    return integer("2r" || b)
                     53: end
                     54: 
                     55: #  exclusive "or" of b1 and b2
                     56: #
                     57: procedure exor(b1,b2)
                     58:    return map(bsum(b1,b2),"2","0")
                     59: end
                     60: 
                     61: #  negation (complement) of b
                     62: #
                     63: procedure neg(b)
                     64:    return map(b,"01","10")
                     65: end
                     66: 
                     67: #  logical "or" of b1 and b2
                     68: #
                     69: procedure or(b1,b2)
                     70:    return map(bsum(b1,b2),"2","1")
                     71: end
                     72: 
                     73: procedure xstop(x)
                     74:    stop("Run-time error 205 in bitstring_
                     75:       \nvalue out of range\noffending value: ",
                     76:       image(x))
                     77: end

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.