Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Peek for Keyboard Input? 2

Status
Not open for further replies.

happyland

Technical User
May 31, 2004
1
US
Browsing through many games I have seen, I wonder what PEEK(1047) does and how to use it. I think it's for keyboard input but I'm not sure. Is it better to use PEEK or INP when it comes to keyboard input? (I saw it used like so: If PEEK(1047) AND 1 then blah blah blah)
 
PEEK usually deals with memory, and the keyboard is a port. The locks are stored in memory, it might deal with one of them (CAPS, Num, Scroll...).
 
The way this is written can be explained in this way
CONST True = -1 , False =0
A%=-1
If a% then Beep else End

Now this seems illogical until you realize that this is a shorthand way of writing
If a% = True then End

Now looking back at the problem we can read it as this

If [Peek(SomeContentOfMemory)] = -1 then end

AND is a bitwise operator that can compare 2 items
Simply it means that if both arguments are TRUE then the Result is true ,so carried out.
This table explains the AND operator
Bit in 1st Expression Bit in 2nd Expression Bit in Result
1 1 1
1 0 0
0 1 0
0 0 0

Hope this helps (btw BASIC uses TRUE as -1 and FALSE as 0 intrinsically) as written in the official QB4.5 Original manual's MicroSoft programmes.

The small code you pointed out simply deletes the keyboard buffer , as most "FAST" games use a keyboard handler to speed up the key reading and do not generally remove keystrokes from the buffer.Hence the computer starts that annoying beeping to indicate too many keys.
Mick
 
*Note:

False is 0

anything other than 0 is True

the reason -1 is considered to be true is this:

0 = 00000000 in binary
-1 = 11111111 in binary

so if you say:
False = 0
True = Not False

True will equal -1

but you can actually use anything <> 0 to be true

Try this

Code:
A = 5
If A then
  Print A
Else
  Print "You Can't See Me!!!"
End If
A = 0
If A Then
  Print "You Can't See Me!!!"
Else
  Print "Not 0 = ";(Not A)
End If

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
-1 = 11111111 in binary

Are you sure? I think it's

-1 = 10000001

but I could be wrong.
 
Actually for 16bit integers it's:
1111111111111111
(but I was making it a little shorter to 8 bit for the example ;-))

The reason is due to the way the processor does it's math

If you want a Quick Reference, open up your windows calculator and set it to scientific mode. press -1 then click the binary radio button.

you get something like:
1111111111111111111111111111111111111111111111111111111111111111
(which is 64bit)

-2 is:
1111111111111111111111111111111111111111111111111111111111111110

-3 is:
1111111111111111111111111111111111111111111111111111111111111101

The reason for this (in 8bit) is:
-1 + 1
Code:
  11111111
+ 00000001
----------
 [COLOR=red][b]1[/b][/color]00000000
The 1 is pushed off to the left, and since it is only 8 bits it leaves you with 00000000 or 0

so...
-1 + 1 = 0

here are a few other examples:
Code:
  00000001  (1)
+ 00000001  (1)
----------
  00000010  (2)

  11111111  (-1)
+ 11111111  (-1)
----------
  11111110  (-2)

(-2) + 5
  11111110  (-2)
+ 00000101  (5)
----------
  00000011  (3)

Hope this help... In one way or another...

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
BTW...

As for -1 = 10000001 in 8 bit...

if the first bit is the sign bit...

00000000 = 0
00000001 = 1
01111111 = 127
10000000 = -128
11111111 = -1

so...
10000001 = -127 ;-)

Don't worry, I thought the same thing for YEARS... until I started to get deep into Assembly and Machine Language for compilers and emulators...

It is common to think that by revesing the sign bit you get the opposite, but as you can see above, that is not the case...

It's just one of those things you never really think about in high level languages... But will get your @$$ handed to you in the LOW level languages if you don't know how it works...

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top