Swap right-most N bits between two integers

时间:2017-08-30 20:11:14

标签: c++ bit-manipulation

This question was asked during an onsite C++ coding interview. I did NOT provide a good answer during that interview. So I was hoping to get feedback and suggestions from folks here at stack overflow. Now that I've had some more time to come up with a solution, I believe that I wrote a decent solution. I think this works OK as along as vin and car.equals() refer to different integers. If registration.equals() and a refer to the same integer, then that integer would be be clobbered to 0.

b

1 个答案:

答案 0 :(得分:6)

I think the intended trick here is

Form1.select()

Note that this uses 3 XOR's, like your solution, but only one masking operation. Plus, this is way friendlier on the CPU registers. There are no false dependency chains. And finally, it works if SELECT number, [A],[B],[C] FROM (select category, number, numberincategory from a) t PIVOT ( SUM([numberincategory]) FOR category IN ([A],[B],[C]) ) AS pvt; .