NVIDIA Interview Question
Software Engineer / Developerse.g A = 10101010
What the instruction "shl $0x1, $A" does is as follows:
CF A
1 <-- 01010100 <-- 0
Now, CFlag is 1.
The logic of the rest 2 instructions is as follows:
If CF is 0, does nothing; Otherwise, set the last bit of A to 1.
In another word, these two instuctions set the last bit of A as the value of CF.
In this example, A will be set as 01010101
Another example: A = 01010111
The first instruction does:
CF A
0 <-- 10101110 <-- 0
Because CF is 0, it will do nothing. The result is already what we want.
Similarly, you can use the instruction "SHR", too.
Sorry that there are two typos in the codes. The correct one should be:
shl $0x1, $A
jnc 1f
or $0x1, $A
1:
And it is gcc assembler syntax. For the intel assembler syntax, it should be
shl A, 1
jnc label1
or A, 1
label1:
there is problem in the piece of code u've posted
say my number is: num = 00111001(binary) which after swapping odd n even bits shud be 00110110
shl num,1 wud make it 01110010
and as there is no carry generated this wud be the result generated and that is wrong.
swapping odd n even bits wud take 5 instuctions atleast. as posted by deepak sharma
XOR with 1's
- goku December 03, 2007