I did some small codes to grab onto my coding after graduation, but I got my stump. Here's the question:
Given the number n and two integers p1, p2, determine whether the bits at positions p1 and p2 are the same or not. Positions p1, p2 and 1 are based.
Example
22,3,2 would be true because it is 0001 0110, because position 2 and 3 are the same.
I decided this one way to convert the decimal fractional to binary and then to string and check if the bits in the positions are the same, but I feel that there is an easier way to do with bit manipulations, but I'm not very good with that. I was thinking if I could just shift the bits to the first position and compare them, I could get an answer, but then I ran into a problem when I shift them to the left, as they simply overflow.
source
share