NVIDIA Interview Question
Software Engineer / DevelopersThis is almost the best solution. Small correction as !k should be included.
#define MACRO(i, j, k) i = ((((i >> j) & 1) ^ !k) << j) ^ i
When jth bit is 0 and k=0 then i think your algo is not working...
ex- i=0x0000001A j=3rd bit k=0
check it
doesn't the question say hex number? won't it effect our code?
if not this will work - #define setbit(i,j,k) i=i^(k<<j)
first set the j'th bit to 0, then add j'th bit to it.
#define MACRO(i,j,k) i = (i & (~(1<<j)) + (k<<j))
#define setbit(i,j,k) i=(k==0)?((1<<j)&i):((1<<j)|i)
- rampurearun September 16, 2012