2 ms·
With the BYTE accessors and binary constants, I find bit and byte manipulation to be far easier in common-lisp than in C. e.g. set the 3 bytes starting at bit
by aidenn0 7y ago
With the BYTE accessors and binary constants, I find bit and byte manipulation to be far easier in common-lisp than in C.
e.g. set the 3 bytes starting at bit 2 of x to the binary value 110:
(setf (ldb (byte 3 2) x) #b110)
or set the 3rd byte (8 bits starting at bit 24) of x to 0xab:
(setf (ldb (byte 8 24) x) #xab)
The first example compiles down to the following 2 instructions on sbcl/x64:
; A4: 4883E1C6 AND RCX, -58
; A8: 4883C930 OR RCX, 48
NOTE: The values are 1 bit shifted from what you might expect (binary 110 = 6; 6 << 2 == 24, not 48) because SBCL uses low-bit tagging to distinguish integers from pointers; working with arrays of unboxed machine-friendly sized integers (8,32,64) works as expected.