I’m trying understand what 0xFF does under the hood in the following code snippet:
if cv2.waitKey(0) & 0xFF == ord('q'): break
0xFF is a hexadecimal constant which is
11111111 in binary. By using bitwise AND (
&) with this constant, it leaves only the last 8 bits of the original (in this case, whatever
It is also important to note that ord(‘q’) can return different numbers if you have NumLock activated (maybe it is also happening with other keys).
For example, when pressing c, the code:
key = cv2.waitKey(10) print(key)
1048675 when NumLock is activated 99 otherwise
Converting these 2 numbers to binary we can see:
1048675 = 100000000000001100011 99 = 1100011
As we can see, the last byte is identical. Then it is necessary to take just this last byte as the rest is caused because of the state of NumLock. Thus, we perform:
key = cv2.waitKey(33) & 0b11111111 # 0b11111111 is equivalent to 0xFF
and the value of key will remain the same and now we can compare it with any key we would like such as your question
if key == ord('q'):
cv2.waitKey() returns a 32 Bit integer value (might be dependent on the platform). The key input is in ASCII which is an 8 Bit integer value. So you only care about these 8 bits and want all other bits to be 0. This you can achieve with:
cv2.waitKey(0) & 0xFF
In this code,
if cv2.waitKey(0) & 0xFF == ord('q'): break
waitKey(0) function returns -1 when no input is made whatsoever. As soon the event occurs i.e. a Button is pressed it returns a 32-bit integer.
0xFF in this scenario is representing binary 11111111 a 8 bit binary, since we only require 8 bits to represent a character we AND
0xFF. As a result, an integer is obtained below 255.
ord(char) returns the ASCII value of the character which would be again maximum 255.
Hence by comparing the integer to the
ord(char) value, we can check for a key pressed event and break the loop.
Truthfully in this case you don’t need 0xFF. If you did
cv2.waitkey(0) == ord(q) it would work all the same.
0xFF is just used to mask off the last
8bits of the sequence and the ord() of any keyboard character will not be greater than 255. You can reference this ASCII Table to find the numerical values of any keyboard character.
ord(c) returns an integer representing the Unicode code point of the character(c) when the argument is a unicode object, or value of the byte when argument is an 8-bit string.
In case of 64-bit systems the value of cv2.waitKey(0) is bitwise AND(&) with the 0xFF hexadecimal constant (which is representation of binary string 11111111) that results in the last 8 bits of it. Thus checking eqality with ord(c).
**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
cv2.waitKey() is the function which bind your code with keyboard and any thing you type will be returned by this function.
output from waitKey is then logically AND with 0xFF so that last 8 bits can be accessed.
Therefore last 8 bit represent input from keyboard and this is compared using == with the character you want.