Bit Shift Operators

I came across such an interview question with programming. But for me it’s not obvious how you know that bit-shift can be used here. Someone will kindly explain. Thank you

The array has size N with integers from 0 to 1024 (repetitions are allowed). Another array of integers has size M with no restrictions on numbers. Find which elements of the first array are present in the second array. (If you are using extra memory, consider minimizing this using bitwise operators)

I would like to know what bitrate operators mean in the real world. And how to identify problems that require a quick-change approach.

Thanks Sanjay

+3
source share
2 answers

. 1025 , , . , , , :

  • 1025 A
  • , A
  • 1025 B
  • int int , B
  • 0 1024: A, B,

1025 , , 8 . , , "k", k/8, k% 8. , ( 0 7) ( 0 1, 1 2, 2 4... 7 128 - ). , 1, - - " ". , 1 < 0 1, 1 < 7 128. :

  • , , , -0 - , C ++: if (array[k / 8] & (1 << (k % 8))) ...it on...
  • 1 ORing - C ++: array[k / 8] |= (1 << (k % 8));

, 1025 , .

+5
+1

All Articles