Intel Corporation interview question

Describe a function (in C) that does the following Input : an integer. Output: if that integer is 4, the function returns 7. if that integer is 7, the function returns 4. constraints: the input is SURE to be either 4 or 7. no need to validate. you are not allowed to use any condition/flow commands (if, switch, while, for, trinary operator...) you are not allowed to use any external library or import anything (like math..) you are not allowed to use %(modulu), * (multiplication)

Interview Answers

Anonymous

6 Jan 2022

The interviewer gave me the constrains each time i thought of a solution. so my first trivial solution was using an "if". than he said "now try without any condition" - so i came up with multiplication (return 38/num) - and than "try without multiplication as well..." and so on. ultimatly there are (i think) 2 good solutions here: 1. return 11 - num 2. return num XOR 3

Anonymous

8 Jan 2022

It's a simple bit manipulation question. The answer is "input xor 0x3". See below for details. 0111 xor 0011 = 0100 0100 xor 0011 = 0111 And it goes on in a loop. xor is an operation that essentially checks if the input values are different, when applied to bits, it means "check if one of the bits is 0 AND the other bit is 1". xor table: A B A xor B 0 0 0 0 1 1 1 0 1 1 1 0

Anonymous

11 Jan 2022

int func(int n) { return 4 | (~(n & 3) & 3); }

Anonymous

12 Jan 2022

Input: x Output: 11-x

Anonymous

30 Jan 2022

XOR with 3