Masking with &?
I have an if that tests the high-bit of the byte:
if(mouse_state.rgbButtons[MOUSE_LEFT_BUTTON] & 0x80 )
i know the & is called masking, but how does masking work (what is it?!), and how does 0x80 translate to "the high bit"?
The & operator is called bitwise AND. It does an AND operation for each bitposition in the operands.
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
the mask 0x80 equals 1000000 in binary code, this makes every bit except the high bit zero in the result and the high bit in the result equals the high bit in the left operand (MOUSE_LEFT_BUTTON)
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
the mask 0x80 equals 1000000 in binary code, this makes every bit except the high bit zero in the result and the high bit in the result equals the high bit in the left operand (MOUSE_LEFT_BUTTON)
0x80 is hexadecimal. 1000000 is binary, not 1 million.
I''m not gonna write a whole thing on binary, hex, and decimal conversion. It''s all over the web. Look around, very useful to know binary and hex for programming.
I''m not gonna write a whole thing on binary, hex, and decimal conversion. It''s all over the web. Look around, very useful to know binary and hex for programming.
0x80 is a hexadecimal number (or just hex) It''s a base 16 number, or a 16 digit number. Our base 10 system has 10 digits 0 through 9. All numbers are a combination of these 10 digits. 9 + 1 = 10. Basic math. Since hex has 16 digits, we need some extra digits to represent those extra numbers, so we use 0 through 9 and A through F (the first 6 characters in the alphabet. In hex, 9 + 1 = A and F + 1 = 10. Computers do everything in binary (base 2). Everything is a 1 or a 0. It converts base 10 and base 16 numbers into binary numbers to process. It converts every bit of information we use into binary to process it. In fact, everything on the computer is stored in binary (and really just converted for our own viewing) As for how these conversions are made, there are entire courses covering that topic. I don''t even remember the basic conversions (binary, base 10, and hex) anymore because I let the computer take care of it for me. That''s the purpose of having all the bitwise manipulators (though understanding how the conversions work help you make far better use of them)
Here is a short table on hex to binary conversion:
hex binary 0 0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 8 1000 9 1001 A 1010 B 1011 C 1100 D 1101 E 1110 F 1111
The numbers in base two are called binary, and you have to convert your numbers to base two. The easiest way to do so is to use your Windows calculator to convert between bases
The difficult way is as follows...
Just think about how the numbering system we use works.. (which is base 10)
''^'' stands for ''to the power of''... you get the idea
1234=1*10^3+2*10^2+3*10^1+4*10^0
which translates to 1000+200+30+4=1234
do you understand how this works?
the same thing works for numbers in base two..
here is how it works...
let''s say you want to convert 23 to binary.. this is how you''d go about it..
you have the following (let''s imagine this is of type byte[unsigned char])
...128 64 32 16 8 4 2 1
okay.. you ask yourself what are all those numbers.. those numbers come from 2^x... let''s see how it works..
2^0=1
2^1=2
2^2=4
2^3=8
2^4=16
2^5=32
2^6=64
2^7=128
...
get the idea? good.. okay.. let''s keep going..
first you have to pick the highest number from that list that is less than the number you have to convert to binary (23 in our case)
i see you shouting "16... 16..."... and yes.. you are correct.. so what do we do now?
well, you simply subtract 16 from 23 and we get 7
23-16=7
now you have a 1 in your binary number... you can make yourself a list of the binary numbers..
Binary number --.
/
V
2^0=1 X /
2^1=2 X \ we haven''t gotten to the X''s YET
2^2=4 X / but we are on our way
2^3=8 X /
2^4=16 1 <--- you have a 1 here because that is the
2^5=32 0 first number that you used
2^6=64 0 (subtracted) from your number...
2^7=128 0
...
now.. we have 7 from the subtraction we did earlier..
what''s the next highest number from the list that is less than the number we have? (7 in this case)
yep, ''4'' is the one we need... so you put 0''s in your binary number on the place of all the X''s up to the row where you have a 4 as the result from 2^2=4.. on the place of that X you put a 1
so here is the list we have at the moment
Binary number --.
/
V
2^0=1 X
2^1=2 X
2^2=4 1
2^3=8 0
2^4=16 1
2^5=32 0
2^6=64 0
2^7=128 0
...
now subtract 4 from 7 and we get 3
7-4=3
what''s the next highest number in the least that is smaller than 3? ''2'' you say.. and you are right... put 0''s up to 2 in the list excluding two (in this case there are no 0''s to put...).. and you put a 1 in the row where you have the 2
and the list...
Binary number --.
/
V
2^0=1 X
2^1=2 1
2^2=4 1
2^3=8 0
2^4=16 1
2^5=32 0
2^6=64 0
2^7=128 0
...
subtract 2 from 3 and we get 1
3-2=1
what''s the next number that is less than or equal to 1 in the list?... 1 of course... so put a 1 on the row with the 1 (which result from 2^0)
so here is the list...
Binary number --.
/
V
2^0=1 1
2^1=2 1
2^2=4 1
2^3=8 0
2^4=16 1
2^5=32 0
2^6=64 0
2^7=128 0
...
subtract 1 from 1 so we get 0...
1-1=0...
and we''re done since we''ve reached 0...
okay, so what''s the binary number that is equal to 23 (base 10) you ask..
well.. you simply start from the bottom and go all the way up each row...and collect the 1''s and 0''s and write them down from left to right..
and we get...
00010111
so
23[decimal]=10111[binary] (we dont need the 0''s at the beginning.. same as 0000235 in decimal.. you only need the 235)
I know all this doesn''t make much sense, but hopefully you''ll get some part of it, and when you read it again later you''ll get the rest of it. Good Luck!
Hope this helps!
..-=ViKtOr=-..
The difficult way is as follows...
Just think about how the numbering system we use works.. (which is base 10)
''^'' stands for ''to the power of''... you get the idea
1234=1*10^3+2*10^2+3*10^1+4*10^0
which translates to 1000+200+30+4=1234
do you understand how this works?
the same thing works for numbers in base two..
here is how it works...
let''s say you want to convert 23 to binary.. this is how you''d go about it..
you have the following (let''s imagine this is of type byte[unsigned char])
...128 64 32 16 8 4 2 1
okay.. you ask yourself what are all those numbers.. those numbers come from 2^x... let''s see how it works..
2^0=1
2^1=2
2^2=4
2^3=8
2^4=16
2^5=32
2^6=64
2^7=128
...
get the idea? good.. okay.. let''s keep going..
first you have to pick the highest number from that list that is less than the number you have to convert to binary (23 in our case)
i see you shouting "16... 16..."... and yes.. you are correct.. so what do we do now?
well, you simply subtract 16 from 23 and we get 7
23-16=7
now you have a 1 in your binary number... you can make yourself a list of the binary numbers..
Binary number --.
/
V
2^0=1 X /
2^1=2 X \ we haven''t gotten to the X''s YET
2^2=4 X / but we are on our way
2^3=8 X /
2^4=16 1 <--- you have a 1 here because that is the
2^5=32 0 first number that you used
2^6=64 0 (subtracted) from your number...
2^7=128 0
...
now.. we have 7 from the subtraction we did earlier..
what''s the next highest number from the list that is less than the number we have? (7 in this case)
yep, ''4'' is the one we need... so you put 0''s in your binary number on the place of all the X''s up to the row where you have a 4 as the result from 2^2=4.. on the place of that X you put a 1
so here is the list we have at the moment
Binary number --.
/
V
2^0=1 X
2^1=2 X
2^2=4 1
2^3=8 0
2^4=16 1
2^5=32 0
2^6=64 0
2^7=128 0
...
now subtract 4 from 7 and we get 3
7-4=3
what''s the next highest number in the least that is smaller than 3? ''2'' you say.. and you are right... put 0''s up to 2 in the list excluding two (in this case there are no 0''s to put...).. and you put a 1 in the row where you have the 2
and the list...
Binary number --.
/
V
2^0=1 X
2^1=2 1
2^2=4 1
2^3=8 0
2^4=16 1
2^5=32 0
2^6=64 0
2^7=128 0
...
subtract 2 from 3 and we get 1
3-2=1
what''s the next number that is less than or equal to 1 in the list?... 1 of course... so put a 1 on the row with the 1 (which result from 2^0)
so here is the list...
Binary number --.
/
V
2^0=1 1
2^1=2 1
2^2=4 1
2^3=8 0
2^4=16 1
2^5=32 0
2^6=64 0
2^7=128 0
...
subtract 1 from 1 so we get 0...
1-1=0...
and we''re done since we''ve reached 0...
okay, so what''s the binary number that is equal to 23 (base 10) you ask..
well.. you simply start from the bottom and go all the way up each row...and collect the 1''s and 0''s and write them down from left to right..
and we get...
00010111
so
23[decimal]=10111[binary] (we dont need the 0''s at the beginning.. same as 0000235 in decimal.. you only need the 235)
I know all this doesn''t make much sense, but hopefully you''ll get some part of it, and when you read it again later you''ll get the rest of it. Good Luck!
Hope this helps!
..-=ViKtOr=-..
thx everyone. i already know how hex and binary works (in fact, i wrote up a whole post on it somewhere here, for someone else ). i just didn''t recognize that 0x80 was hex. but now that i think about it, that is the format used to specify a hex 80 and not a decimal 80.
thx again.
thx again.
You made me type all that to tell me that you already know how to conver them? ;P
..-=ViKtOr=-..
..-=ViKtOr=-..
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement