And Or Xor

 BBS: Inland Empire Archive
Date: 09-21-92 (21:38)             Number: 104
From: RICK PEDLEY                  Refer#: NONE
  To: REUVEN LAX                    Recvd: NO  
Subj: And Or Xor                     Conf: (2) Quik_Bas
 On 09-20-92 Reuven Lax wrote to All...

 RL> I recently posted a question asking how to use AND OR XOR
 RL> to set and rest and look at bits since  then Ive figured out
 RL> how to do that can sombody tell me what other uses it may have?

Many uses -- you've only looked at the fundamental way in which
it works.  Here's an example of something I've used.  In most
programming books, they show you how to determine whether a
number is odd like this:

INPUT "Enter a whole number: ", N
IF N/2 <> N\2 THEN PRINT N "is odd."

So 5/2 = 2.5 and 5\2 = 2 --> 5 is odd.
   8/2 = 4   and 8\2 = 4 --> 8 is not odd.
A more elegant and faster method is replace the second line with

IF N AND 1 THEN PRINT N "is odd."

If the rightmost bit in N is set, then N must be odd, and N AND 1
evaluates to TRUE.

XOR can be used as a simple encryption method.

INPUT "Type something here: ", A$
Key = 221 'Pick any integer between 1 and 255
Code$ = ""
FOR x = 1 TO LEN(A$)
   Code$ = Code$ + CHR$(ASC(MID$(A$,x,1)) XOR Key)

To get the original string back, XOR Code$ with =the same number=.

Key = 221 'Pick any integer between 1 and 255
A$ = ""
FOR x = 1 TO LEN(Code$)
   A$ = A$ + CHR$(ASC(MID$(Code$,x,1)) XOR Key)

... OFFLINE 1.40 * It's getting cold in here...someone turn
on some more DX2'2!

--- Maximus 2.01wb
 * Origin: The BULLpen BBS * Intel 14.4EX (613)549-5168 (1:249/140)
Outer Court
Echo Basic Postings

Books at Amazon:

Back to BASIC: The History, Corruption, and Future of the Language

Hackers: Heroes of the Computer Revolution (including Tiny BASIC)

Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution

The Advent of the Algorithm: The Idea that Rules the World

Moths in the Machine: The Power and Perils of Programming

Mastering Visual Basic .NET