Reputation: 2619
I want to make program to multiply two numbers by add and shift method. I have written this code in sbcl lisp.
(defun calculator (num1 num2)
(write-line "In the function")
(let ((res 0))
(loop for lpr from 0 to 63
do (let ((end-bit (logand num2 1)))
(format t "res is : ~a. ~%" num2)
(if (= end-bit 1)
(+ res num1))
(ash num2 -1)
(ash num1 1)
(format t "after ash ~a ~%"num2)))
(format t "result is ~a.~%" res)))
(let ((num1 (progn
(write-line "Enter first number: ")
(finish-output)
(read)))
(num2 (progn
(write-line "Enter second number: ")
(finish-output)
(read))))
(if (or (= num1 0) (= num2 0))
(write-line "result is 0.0")
(calculator num1 num2)))
but the value of res, num2, num1, end-bit variables remains same throughout the program.I think that the logical and bitwise operations are not happening.What's the problem.
Upvotes: 0
Views: 139
Reputation: 17517
None of the functions +
or ash
do their thing in-place, meaning you have to set that result back to the variable.
So you want to update a var, do it like this:
(setf num2 (ash num2 -1))
For increments and decrements there's an in-place variant called incf
:
(incf res num1) ; (setf res (+ res num1))
Upvotes: 5