Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Return a single value using AND OR...Help.
#1
I have a function that detects if a key is pressed and it returns a value. How do I make the function return that UP and LEFT were pressed? I've seen in other people's code the use of AND OR or something, so could someone help me. DOWN = 1 RIGHT = 2 UP = 3 LEFT = 4.
am: "Where should I put this thing so that it doesn't hurt anyone we know or care about?"
Max:"Out the window, Sam. There's nobody but strangers out there."
Reply
#2
you need to use separate digits of any system.

for instance, if you wanted to use arabic numerals:

1010 - one thousand and ten.

but for total efficiency, use binary:

1010 - ten.

to read a binary digit:

is_it_on = x and (2^digit - 1) - qbasic
is_it_on = x & (exp(2, digit) - 1); - c/c++ [i think]
i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Reply
#3
I made the function return a string and then I parse the string for commands that it gave. But it will still only take one command at a time. Here's code:

Code:
std::string s = getInput();
    if (!s.find("RIGHT")) {
        xv+= ACCEL;
    }
    if (!s.find("UP")) {
        y--;
        //yv-= ((ACCEL * 255) * YFRICTION);
    }
Code:
std::string cBubble::getInput()
{
    std::string s;
    if (key[KEY_ESC]) return "ESC";
    if (key[KEY_RIGHT]) s+="RIGHT";
    if (key[KEY_LEFT]) s+="LEFT";
    if (key[KEY_DOWN]) s+="DOWN";
    if (key[KEY_UP]) s+="UP";
    return s;
}
am: "Where should I put this thing so that it doesn't hurt anyone we know or care about?"
Max:"Out the window, Sam. There's nobody but strangers out there."
Reply
#4
I would do something like this:

Code:
UP = bit 1
DOWN = bit 2
LEFT = bit 3
RIGHT = bit 4

Then I just...

Code:
pressed = 0;

if (key[KEY_UP])
   pressed = pressed | 1;
else
   if (key[KEY_DOWN])
      pressed = pressed | 2;

if (key[KEY_LEFT])
   pressed = pressed | 4;
else
   if (key[KEY_RIGHT])
      pressed = pressed | 8;

return pressed;

Then, to check if left, for example:

Code:
if ( (pressed & 4) == 4 )
   // left was pressed.

if ( (pressed & 5) == 5)
   // left and up were pressed ( 4 + 1 = 5, bits 1 _AND_ 3).

Bit #n's power of two is calculated this way (n ranges from 1 to...):

Code:
power_of_two = 1 << (n-1)
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#5
Awesome. That's exactly what I wanted. I got it running, but didn't use that last part. What is that and what does it do?

Quote:Bit #n's power of two is calculated this way (n ranges from 1 to...):

Code:
power_of_two = 1 << (n-1)

and if I wanted to add in other options to it, I just add in another number 2x the size of the last one?
am: "Where should I put this thing so that it doesn't hurt anyone we know or care about?"
Max:"Out the window, Sam. There's nobody but strangers out there."
Reply
#6
It calculates a power of two without multiplying.

When you are dealing with bits 1, 2, 3 and stuff you will remember the values (1,2,4,8...) but which number will you use to AND/OR to modify bit #27? Wink

Code:
mask = 1 << (27-1);

This is used if the bit # is a variable. To print a number (unsigned short 16 bits integer) in binary:

Code:
for (i=16;i>=1;i++)
{
   mask = 1<<(i-1);
   printf("%i", (number & mask) / mask);
}
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#7
<< =Shift Left

>> = shift Right
y smiley is 24 bit.
[Image: anya2.jpg]

Genso's Junkyard:
http://rel.betterwebber.com/
Reply
#8
Quote:<< =Shift Left

>> = shift Right
isn't >>> the right shift that doesn't wrap?
am an asshole. Get used to it.
Reply
#9
erm, afaik >> doesnt wrap. It just kicks off the extra bits.
b]Hard Rock[/b]
[The Stars Dev Company] [Metal Qb flopped] [The Terror]
Stop Double Posts!
Whats better? HTML or Variables?
Reply
#10
Quote:
relsoft Wrote:<< =Shift Left

>> = shift Right
isn't >>> the right shift that doesn't wrap?

Shr and Shl does not wrap

there is a Rotate Bits function in ASM. Dunno the C flavor though.
y smiley is 24 bit.
[Image: anya2.jpg]

Genso's Junkyard:
http://rel.betterwebber.com/
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)