Question d’entretien chez Google

Bitweiser zyklischer Rechtsshift

Réponses aux questions d'entretien

Utilisateur anonyme

3 janv. 2011

In Java: public int CyclicRightShift(int integer, int shift) { return (integer>>shift); }

2

Utilisateur anonyme

15 janv. 2011

Fish, you assume that you know the number of bits (32 in this case). What if you don't know?

1

Utilisateur anonyme

25 janv. 2011

template public int CyclicRightShift (T value, int shift) { int bitCount = sizeof(value) * 8; return (value > shift); }

1

Utilisateur anonyme

20 févr. 2011

10001111010 11101010001 n<<5 0xF8000000 (n<

Utilisateur anonyme

30 déc. 2014

This problem has a simple solution given the following: Suppose that k is the value we want to shift, it is a 32-bit integer and we want to shift it n times to the right. 1. Regular shifting usually zeroes out values that are shifted out of the original, in both directions. e.g. 000101 >> 1 = 000010 (or 5 shifted 1 times to the right gives 2) 2. We want the zeroed out values to be appended in the same order at the beginning of the resulting value, thus, we can shift them left a number of times that is equal to the original value's bitsize minus the number of shifts we want to do. The code is: public int bitWiseShift(int k, int shifts){ if(shifts == 0) return k; return k >> shifts | k << 32 - shifts; }