How would you divide a number by 3 without using *, /, +, -, %, operators?
The number may be signed or unsigned.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is a simple function which performs the desired operation. But it requires the
+operator, so all you have left to do is to add the values with bit-operators:As Jim commented this works, because:
n = 4 * a + bn / 3 = a + (a + b) / 3So
sum += a,n = a + b, and iterateWhen
a == 0 (n < 4),sum += floor(n / 3);i.e. 1,if n == 3, else 0