chickadee » srfi-151 » list->bits

list->bits listprocedure
vector->bits vectorprocedure

Returns an integer formed from the booleans in list/vector, using the first element as bit #0, the second element as bit #1, and so on. It is an error if list/vector contains non-booleans. A 1 bit is coded for each #t; a 0 bit for #f. Note that the result is never a negative integer.

   (list->bits '(#t #f #t #f #t #t #t)) => #b1110101
   (list->bits '(#f #f #t #f #t #f #t #t #t)) => #b111010100
   (list->bits '(#f #t #t)) => 6
   (list->bits '(#f #t #t #f)) => 6
   (list->bits '(#f #f #t #t)) => 12

   (vector->bits '#(#t #f #t #f #t #t #t)) => #b1110101
   (vector->bits '#(#f #f #t #f #t #f #t #t #t)) => #b111010100
   (vector->bits '#(#f #t #t)) => 6
   (vector->bits '#(#f #t #t #f)) => 6
   (vector->bits '#(#f #f #t #t)) => 12

For positive integers, bits->list and list->bits are inverses in the sense of equal?, and so are bits->vector and vector->bits.