What the hell is the 0xFF? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What the hell is the 0xFF?

24th Sep 2017, 9:07 AM
Adem şahin
Adem şahin - avatar
3 Answers
+ 8
255! Just written in hexadecimal. In hexadecimal, you have 16 digits instead of our decimal 10, and you would count like 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F, 10,11,12,13,14,15,16,17,18,19,1A,1B,1C,1D,1E,1F, ... 90,91,...,9E,9F, A0,A1,...,AE,AF, ... F0,...,FF, 100 and you'll find FF is the 255th number in that list. To distinguish between decimal and hexadecimal in programming, we prefix hex numbers with "0x".
24th Sep 2017, 9:18 AM
Schindlabua
Schindlabua - avatar
+ 2
Computers store everything as 1s and 0s, right? That's called binary and like you'd have 10 digits in decimal or 16 digits in hexadecimal, in binary we have just two - 0 and 1 - and you would count like 0,1,10,11,100,101,110,111,1000,... and since 16 = 2*2*2*2, it turns out that one hexadecimal digit is exactly 4 binary ones. So we use hexadecimal to write binary numbers but shorter. In particular (I'll prefix binary with 0b): 0xF = 0b1111 0xFF = 0b11111111 See how it's all 1s? That's important for when we use '&'. '&' will look at two numbers in binary and compare each digit - if the digit in both numbers is 1, it returns 1, otherwise 0. Let's look at, for example, 4093 & 0xFF, rewritten in binary: 0b111111111101 (4093) & 0b000011111111 (255 or 0xFF) = ________________________________ 0b000011111101 (253) I hope that makes sense. A way to look at this is that for all the bits (binary digits) that are set to 1 in the second number, we copy whatever to the bits in the first number are. In the particular case of 0xFF / 255 / 0b11111111 that means we copy the lowest 8 bits of a number. It also happens to be that for 0xFF (and 0xFF only) x & 0xFF is the same as x % 255 I know it's a lot to take in if you haven't encountered hex or binary before and I've skipped some details but I hope you got something out of it.
25th Sep 2017, 6:06 PM
Schindlabua
Schindlabua - avatar
0
It is used in logic expressions such as: while statement A & 0xFF: what is the connection?
25th Sep 2017, 2:05 PM
Adem şahin
Adem şahin - avatar