Pergunta de entrevista da empresa Google

Bitweiser zyklischer Rechtsshift

Respostas da entrevista

Sigiloso

3 de jan. de 2011

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

2

Sigiloso

15 de jan. de 2011

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

1

Sigiloso

25 de jan. de 2011

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

1

Sigiloso

20 de fev. de 2011

10001111010 11101010001 n<<5 0xF8000000 (n<

Sigiloso

30 de dez. de 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; }