Changeset 39979 in project


Ignore:
Timestamp:
04/12/21 23:53:35 (5 weeks ago)
Author:
Kon Lovett
Message:

fix integer<->list (or at least make invertable)

Location:
release/5/bitwise-utils/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • release/5/bitwise-utils/trunk/bitwise-utils.egg

    r39887 r39979  
    33
    44((synopsis "Bitwise utilities")
    5  (version "1.2.1")
     5 (version "1.2.2")
    66 (category data)
    77 (author "Kon Lovett")
  • release/5/bitwise-utils/trunk/bitwise-utils.scm

    r39177 r39979  
    4242(import scheme
    4343        (only (chicken base) declare sub1 add1 fixnum? foldl cut)
     44        (only (chicken fixnum) most-positive-fixnum)
    4445        (chicken type)
    4546        (chicken foreign)
     
    7071(: bitwise-field-reverse    (integer fixnum fixnum --> integer))
    7172(: bitwise-field-rotate     (integer fixnum fixnum fixnum --> integer))
    72 (: integer->list            (integer #!optional (or boolean fixnum) --> (list-of fixnum)))
     73(: integer->list            (integer #!optional fixnum --> (list-of boolean)))
    7374(: list->integer            (list --> integer))
    7475
     
    130131        n = COUNT( n, 3 );
    131132        n = COUNT( n, 4 );
    132         C_return( (unsigned int) COUNT( n, 5 ) );")) )
     133        n = COUNT( n, 5 );
     134        C_return( (unsigned int) n );")) )
    133135  (else ;32bit
    134136    (define uword-bitwise-count
     
    139141        n = COUNT( n, 2 );
    140142        n = COUNT( n, 3 );
    141         C_return( (unsigned int) COUNT( n, 4 ) );")) ) )
     143        n = COUNT( n, 4 );
     144        C_return( (unsigned int) n );")) ) )
    142145
    143146#|
     
    171174  (do ((m (bitwise-abs n) (arithmetic-shift m -1))
    172175       (k (sub1 k) (sub1 k))
    173        (rvs 0 (bitwise-ior (arithmetic-shift rvs 1) (bitwise-and 1 m))))
     176       (rvs 0 (bitwise-ior (arithmetic-shift rvs 1) (bitwise-and 1 m))) )
    174177      ((negative? k) (if (negative? n) (bitwise-not rvs) rvs))))
    175178
     
    209212            (bitwise-and (bitwise-not (arithmetic-shift mask start)) n)) ) )
    210213
    211 (define (integer->list k #!optional len)
    212   (if (not len)
    213     (do ((k k (arithmetic-shift k -1))
    214          (lst '() (cons (add1 k) lst)) )
    215         ((<= k 0) lst))
    216     (do ((idx (sub1 len) (sub1 idx))
    217          (k k (arithmetic-shift k -1))
    218          (lst '() (cons (add1 k) lst)))
    219         ((negative? idx) lst)) ) )
     214(define (integer->list k #!optional (len most-positive-fixnum))
     215  (do ((idx len (sub1 idx))
     216       (k k (arithmetic-shift k -1))
     217       (lst '() (cons (not (zero? (bitwise-and k 1))) lst)) )
     218      ((or (<= k 0) (<= idx 0)) lst)) )
    220219
    221220(define (list->integer bools)
    222221  (do ((bs bools (cdr bs))
    223        (acc 0 (+ acc acc (if (car bs) 1 0))))
     222       (acc 0 (+ acc acc (if (car bs) 1 0))) )
    224223      ((null? bs) acc)) )
    225224
  • release/5/bitwise-utils/trunk/tests/bitwise-utils-test.scm

    r39177 r39979  
    6868(test 28 (bitwise-count #x-b0000000000000000000000000110000deadbeef))
    6969
     70(test #b011001100110011001100110011001100110011001100110 (list->integer (integer->list #b011001100110011001100110011001100110011001100110)))
     71(test #b0 (list->integer (integer->list #b01100110 0)))
     72(test #b110 (list->integer (integer->list #b01100110 3)))
     73
    7074;;;
    7175
Note: See TracChangeset for help on using the changeset viewer.