Question d’entretien chez Amazon

Implement a program to find whether a number is even or odd without using arithemetic operator

Réponses aux questions d'entretien

Utilisateur anonyme

24 févr. 2013

Mask off the least significant bit. in C, for instance: /* return 0 if even or 1 if odd */ char isOdd(char c) { return c & 0x01; } One side note... there is a whole class of problems that are slightly less trivial, such as determining if a character is uppercase or a character is a vowel or what-not. Brute force is your friend here, and the quickest way to do this is to come up with a table for your character set (not always 8 bits) that is a truth table for all combinations. You are sacrificing space for speed.

Utilisateur anonyme

3 mars 2013

This can be solved by checking the last bit of the number. if it is 0 then even else odd. So, if number is n. You can do n&1 and then check whether this number is 0 or not.

8