[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: #<byte>?
Lo, on Saturday, 26 August, 2000, John R. Hall did write:
<SNIP>
> Perhaps some operators for bit shifting and masking would also
> be helpful.
These, at least, exist; search through the MzScheme docs for
bitwise-{and,ior,xor,not}
arithmetic-shift
Given that, it would be pretty straightforward to implement a byte type;
something like this should work, if you want to treat bytes as a separate
type.
(define-struct byte (value))
(define integer->byte
(lambda (n)
(make-byte (bitwise-and n #xFF))))
(define signed-integer->byte
(lambda (n)
(let ([sign (if (negative? n) #x80 #x00)])
(make-byte (bitwise-or (bitwise-and n #x7F) sign)))))
(define byte->integer
(lambda (b)
(byte-value b)))
(define signed-byte->integer
(lambda (b)
(let* ([value (byte-value b)]
[sign (if (zero? (bitwise-and value #x80))
1
-1)])
(* (bitwise-and value #x7f) sign))))
(Warning: the above code was written on the spot; I haven't tested it or
even checked it for syntactic correctness!)
The only thing missing is a means to do binary I/O on these objects; I
suspect you might have to go out to C for that.
Richard
- References:
- #<byte>?
- From: "John R. Hall" <overcode@lokigames.com>