I\'m trying understand what 0xFF does under the hood in the following code snippet:
if cv2.waitKey(0) & 0xFF == ord(\'q\'):
break
A
**READ THIS IT WILL SAVE YOUR TIME **
Note 1: cv2.waitKey() will return the keyword that you press, in case if u just click on the close button when the window is opened then it will return -1
Note 2: let us assume that you have pressed 'q', then cv2.waitkey() will return that 'q' but the format it returns will be in string data type in order to change it to binary we are performing bitwise AND operation(&) with 0xFF which is in hexadecimal format also know as hexadecimal constant, which is 255 in decimal or 11111111 in binary.
Decimal-255
Binary-11111111
Hexadecimal-0xff
**Note it is the same value in different formats **
Note 3: we all know that '&' in python is used to perform bitwise 'And' operation,
**Bitwise in the sense we perform the And operation at binary level **
AND operation logic:
0&0=0
0&1=0
1&0=0
1&1=1
below represents the small table of asci value and binary value of letter 'q'
**Letter ASCII Code Binary **
q 113 01110001
Note 4: since we have given the hexadecimal constant **0xFF** whose value in binary is 11111111, let's perform the bit AND OPERATION with the binary value of letter 'q' which
is 01110001.
q= 01110001
0xFF=11111111
----------
01110001 ----->q so when do bitwise and operation we get the same value of q
----------
Note 5: since we are performing bitwise And operation with 0xFF which is a hexadecimal constant, ** once the bitwise operation is completed or performed, the result will change to the decimal format, so since we are using ord('q') function which will return the decimal value or ASCII value of 'q', so both will be equal the condition if condition becomes true and the loop will break