[r6rs-discuss] [Formal] right shift formula and bit-field signedness
daniel_villeneuve at sympatico.ca
Thu Feb 22 23:27:03 EST 2007
This message is a formal comment which was submitted to formal-comment at r6rs.org, following the requirements described at: http://www.r6rs.org/process.html
submitter's name: Daniel Villeneuve
submitter's email address: daniel_villeneuve at sympatico.ca
type of issue: Defect
R6RS component: Exact bitwise arithmetic
version of the report: 5.92 lib
Several fixes to the bitwise library.
This comment covers several (9) items, not all with the same importance.
1) I assume that the intent of all bitwise procedures is to return exact
integers. If this is so, the formula used to define
bitwise-arithmetic-shift does not cover the case of negative ei2. A
possible one would be:
(->exact (floor (* ei1 (expt 2 ei2))))
This is akin to sign-extending right shift, so
(bitwise-arithmetic-shift -6 -1) => -3
(bitwise-arithmetic-shift -5 -1) => -2
(bitwise-arithmetic-shift -4 -1) => -2
(bitwise-arithmetic-shift -3 -1) => -2
(bitwise-arithmetic-shift -2 -1) => -1
(bitwise-arithmetic-shift -1 -1) => -1
2) Bit fields are always defined using a finite number of bits, contrary
to 2's complement representation which implicitly uses an infinite
extension of $0$ bits or $1$ bits to the left.
Either a) we specify that the leftmost value bit of a bit-field is
understood to be repeated infinitely to the left, or b) that $0$ bits
are used no matter the value of the leftmost bit. With a), we can have
negative values as output to bitwise-bit-field but we have problems with
0-bit and 1-bit bit fields, with shifting and masking, etc. With b), we
only have positive bit fields.
I suggest that b) is more useful/intuitive:
(bitwise-bit-field #b00101100 0 6) 8) => a) #t; b) #f
(bitwise-bit-field #b00101100 0 6) 4) => a) -2; b) 2
(bitwise-bit-field #b00101100 0 7) 4) => a) 2; b) 2
Either way, a clarification on the signedness of bit-fields should
be added to the introduction.
3) The domain of bitwise-rotate-bit-field should restrict ei2 and ei3
as for the other bit-field procedures, and ei4 can be left
unrestricted (though non-negative), as this is both intuitive and
supported by the proposed implementation.
4) The primitive bitwise-zero? is used in the implementation of
bitwise-bit-set? but is not defined. It should be replaced with zero?.
5) The primitive bitwise-negative? is used in the implementation of
bitwise-length but is not defined. It should be replaced with
6) The spec of bitwise-length should start as "Returns ..." instead
of "These procedures return ...".
7) The spec of bitwise-bit-set? should start as "Returns ..." instead
of "Otherwise, returns ...". There seems to be an extra newline after
8) General: replace "The XXX procedure returns ..." by "This procedure
returns ..." when unambiguous, for homogeneity.
9) In the spec of bitwise-reverse-bit-field, replace "from the ei1"
by "from ei1".
More information about the r6rs-discuss