Jump to content

Bitwise operations: Difference between revisions

Line 672:
; no logical shift
)</lang>
 
Left and right logical shift may be implemented by the following functions:
 
<lang lisp>
(defun shl (x width bits)
"Compute bitwise left shift of x by 'bits' bits, represented on 'width' bits"
(logand (ash x bits)
(1- (ash 1 width))))
 
(defun shr (x width bits)
"Compute bitwise rigth shift of x by 'bits' bits, represented on 'width' bits"
(logand (ash x (- bits))
(1- (ash 1 width))))
</lang>
 
Left and right rotation may be implemented by the following functions:
 
<lang lisp>
(defun rotl (x width bits)
"Compute bitwise left rotation of x by 'bits' bits, represented on 'width' bits"
(logior (logand (ash x (mod bits width))
(1- (ash 1 width)))
(logand (ash x (- (- width (mod bits width))))
(1- (ash 1 width)))))
 
(defun rotr (x width bits)
"Compute bitwise rigth rotation of x by 'bits' bits, represented on 'width' bits"
(logior (logand (ash x (- (mod bits width)))
(1- (ash 1 width)))
(logand (ash x (- width (mod bits width)))
(1- (ash 1 width)))))
</lang>
 
=={{header|D}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.