#define DIKEYDOWN(data n) (data[n] & 0x80)
What I'm wondering is why is comparing data[n] to 0x80.
And also, is 0x80 arbitrary?
Edited by - Brian Jones on March 9, 2002 9:29:15 PM
bitwise & operator
I'm not really sure if I understand this concept, but the bitwise & operator tests each bit in operand right? From left to right, correct? (True and false wise)
So if you had something like this
0x6A4B
&
0x6F0B
________
The outcome would be
0x600B
....right?
Now my second problem, I'm reading the Direct Input chapter in
TOTWGPG, and the author has this macro
I don't have a signature
0x6A4B & 0x6F0B = 0x6A0B
1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0
Take a look at 0x80 in binary using the windows calculator. Notice that the first bit is 1 and all others are 0. This is the sign bit. If this bit is set the value represents a negative number.
How that plays into the specifics of that macro I don''t know.
1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0
Take a look at 0x80 in binary using the windows calculator. Notice that the first bit is 1 and all others are 0. This is the sign bit. If this bit is set the value represents a negative number.
How that plays into the specifics of that macro I don''t know.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
He basically has this..
Does this mean that if if the escape key is pressed at the time of the function call, the DIK_ESCAPE element of keyboardstate is turned to 1? Also, you say the first number in 0x80 is 1 followed by 0s, so when the if statement evaulates the expression "DIKEYDOWN(keyboardstate, DIK_ESCAPE)", it is evaluating the first bit of keyboardstate[DIK_ESCAPE] which is 1
since it was pressed, and compares it to the first bit of 0x80, which is also 1, and that should return a 1 to the if statement
which evaulates 1 as true. Is that correct?
Please say it is...
char* keyboardstate[256]lpDirectInputDevice->GetDeviceState(sizeof(keyboardstate),&keyboardstgate);if(DIKEYDOWN(keyboardstate, DIK_ESCAPE)){/*Do something*/}
Does this mean that if if the escape key is pressed at the time of the function call, the DIK_ESCAPE element of keyboardstate is turned to 1? Also, you say the first number in 0x80 is 1 followed by 0s, so when the if statement evaulates the expression "DIKEYDOWN(keyboardstate, DIK_ESCAPE)", it is evaluating the first bit of keyboardstate[DIK_ESCAPE] which is 1
since it was pressed, and compares it to the first bit of 0x80, which is also 1, and that should return a 1 to the if statement
which evaulates 1 as true. Is that correct?
Please say it is...
I don't have a signature
March 09, 2002 05:13 PM
quote: Original post by Brian Jones
I''m not really sure if I understand this concept, but the bitwise & operator tests each bit in operand right? From left to right, correct? (True and false wise)
So if you had something like this
0x6A4B
&
0x6F0B
________
The outcome would be
0x600B
....right?
Now my second problem, I''m reading the Direct Input chapter in
TOTWGPG, and the author has this macro
#define DIKEYDOWN(data n) (data[n] & 0x80)
What I''m wondering is why is comparing data[n] to 0x80.
And also, is 0x80 arbitrary?
In this case 0x80 is what''s called a "mask" you use a mask when you want to test for the values of specific bits. You can figure out which bit/bits the programmer is testing for by converting the 0x80 into it''s binary equivilent. In this case 0x80 = 10000000 in binary. If you look in the documentation for the function you''ll see this:
"The return value specifies the status of the specified virtual key, as follows:
If the high-order bit is 1, the key is down; otherwise, it is up.
If the low-order bit is 1, the key is toggled. A key, such as the CAPS LOCK key, is toggled if it is turned on. The key is off and untoggled if the low-order bit is 0. A toggle key''s indicator light (if any) on the keyboard will be on when the key is toggled, and off when the key is untoggled. "
This gives us a minor problem. You see we''ve got a bit field that looks like this: 0000000 . When you press a key such as the space bar it looks like this: 10000000. If the caps lock is on then it looks like this: 00000001. Now if a key is pressed & caps lock is on it will look like this: 10000001.
Now you should know by now that a Boolean value is either True (which equils 1) or False (which equils 0). What the author wants to do is create a macro that will tell us when a key is down by returning either a true value (1) or a false value (0). But as you can see from the above bit field is that the only time you''ll get a 0 is if caps lock or similar is not on and a key is not pressed. And the only time You''ll get a 1 is if the caps lock is on and no key is being pressed. Obviously this is not what you want. So lets see what happens when we add our mask into the mix.
remember 0x80 is our mask and in binary it looks like this 1000000.
No keys are pressed and no caps lock/num lock is on:
0000000 & 1000000 = 0000000
A key is pressed and no caps lock/num lock is on:
10000000 & 10000000 = 0000001
Caps lock/ num lock is on and no keys are pressed:
00000001 & 10000000 = 0000000
Caps lock/ num lock is on and a key is pressed:
10000000 & 10000001 = 0000001
now I''m sure your probably asking yourself why the bits are reversed? Well when we evaluate each bit we move from right to left. The first bits compaired are the two bits to the right. Then we move over to the next and so on. Which is how we get our needed 1(true) and 0(false) values.
By high-order and low-order bits, do you mean the first and last digits of the binary number?
I don't have a signature
Yes. Sometimes its also refered to as "most significant bit" (msb) and "least significant bit" (lsb).
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
I''m sure you know this already, but in C, 0 is false and everything else is true. that''s the a key point -- any NONZERO value is true -- not just 1.
Thus, &''ing something with the mask will make all bits except the mask bit 0.
1110
&
0010 //mask!
-----
0010 //this is a non-zero value thus
if (0010)
{
printf ("hi");
}
if (1110 & 0010)
{
printf ("hi");
}
will both print out print "hi" because they both evaluate to non-zero values.
however:
1100
&
0010 //mask!
-----
0000 //this is a zero value
if (0)
{
printf ("i");
}
or
if (0010 & 1100)
{
printf("i");
}
neither will print i because both evaluate to 0 or false.
(this is psuedo code... it will not work because the binary values will be interpretted as decimal values.)
DmGoober
Thus, &''ing something with the mask will make all bits except the mask bit 0.
1110
&
0010 //mask!
-----
0010 //this is a non-zero value thus
if (0010)
{
printf ("hi");
}
if (1110 & 0010)
{
printf ("hi");
}
will both print out print "hi" because they both evaluate to non-zero values.
however:
1100
&
0010 //mask!
-----
0000 //this is a zero value
if (0)
{
printf ("i");
}
or
if (0010 & 1100)
{
printf("i");
}
neither will print i because both evaluate to 0 or false.
(this is psuedo code... it will not work because the binary values will be interpretted as decimal values.)
DmGoober
Alexander "DmGoober" Jhinalexjh@online.microsoft.com[Warning! This email account is not attended. All comments are the opinions of an individual employee and are not representative of Microsoft Corporation.]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement