Understanding Binary Operations
From: Michael Current (aa700@cleveland.Freenet.Edu)
Date: 01/28/92-10:33:39 PM Z
- Next message by date: Michael Current: "SuperArc, Disk Communicator"
- Previous message by date: Michael Current: "TUTORIAL: CIOV"
- Return to Index: Sort by: [ date ] [ author ] [ thread ] [ subject ]
From: aa700@cleveland.Freenet.Edu (Michael Current) Subject: Understanding Binary Operations Date: Tue Jan 28 22:33:39 1992 Reprinted from the A.C.E.C. BBS (614)-471-8559 ----------------------------------- Understanding Binary Operations ----------------------------------- The 6502 Processor, the CPU which all Atari, Apple, and Commodore 8-bit computers use, supports three bit manipulation functions. These functions can be very powerful in various circumstances--in both BASIC and especially Machine Language. Though Atari BASIC does not truly support these functions, BASIC XE and other similar extentions of BASIC do offer them to the BASIC user. It is assumed throughout this article that the user is familiar with binary and decimal, as well as conversions between them. One term must be defined before we go further. A "mask" is a number that is used with any of these operations. You must have a mask to use them. First we will cover the AND function. This function is very much self explanatory. If the bit in the original number AND in the mask are 1, then the result will be 1. If the bit in either of the numbers is 0, then the resulting bit will be 0. To demonstrate this (and all of these functions), we will look at it in both binary and decimal notation. Bit "Identifier": ABCDEFGH ------------------------------- Original Number: 51 = 00110011 Mask Number: 15 = 00001111 Resulting Value: 3 = 00000011 This says "The value 51 ANDed with 15 is 3". As you can see, bits G and H contain 1's in BOTH the original number and the mask. Because of this, bis G and H are set in the resulting value. Bits E and F are not set because the original number contains 0's in these bits. Bits C and D are not set because the mask number contains 0's in these bits. THE AND FUNCTION IS GENERALLY USED TO ZERO OUT UNWANTED BITS. For example, if you are getting input but are not interested in whether or not it is inverse (that is, has an ATASCII value of 128 or more), then you could AND the input with 127. This will cause all input to be normal, un-inverse characters. In Machine Language, the format is: AND #mask. The Acumulator will then be ANDed with the mask and the result will be stored in the accumulator. Next we will deal with the OR function. With this function, if a bit is set in EITHER OR BOTH the original or the mask, then the resulting bit will be set. Here is our demo of this function: Bit "Identifier" ABCDEFGH ------------------------------- Original Value: 65 = 01000001 Mask Value: 192 = 11000000 Resulting Value: 193 = 11000001 As you can see, bit A, B, and H are set in the resulting value. Bit H is set because it was set in the original value. Bit B is set because it was set in BOTH the original and the mask. Bit A was set because it was set in the mask. THE OR FUNCTION IS GENERALLY USED TO FORCE SET A BIT. If you wanted to make all characters inverse, you would simply OR it with the value of 128. In Machine Language, the format is: ORA #mask. The value in the accumulator will be ORed with the mask and the result will be stored in the accumulator. Finally we have the Exclusive OR function (generally shortened to EOR). With the EOR function, the bit is "flipped" if the MASK has a 1 in it. If the mask has a 0, then the bit is left unchanged. Here is the demonstration: Bit "Identifier": ABCDEFGH ------------------------------- Original Value: 193 = 11000001 Mask Value: 130 = 10000010 Resulting Value: 67 = 01000011 In the result, bit H is set because bit H was 1 in the original. Since the mask contained a 0, that bit was left unchanged. Bit G contains a 1. This is because Bit G had a 0 in the original value, but since it had a 1 in the mask, the 0 was "flipped", thus becoming the 1. Bits C, D, E, and F are 0 because the 0's in the mask indicate that the bits should be left alone, thus the bits assume the value they had in the original number. Bit B is 1 since the 0 in the mask indicates that the bit should not be changed, and therefore remains the same as in the original. Bit A is 0 because the 1 in the mask says that the original bit should be flipped: and "flipping" a 1 will result in a 0. This function is not used as much as the other two functions but can be useful in some instances. A very basic use of the function would be to flip the value of a flag. These functions may seem a bit complex, and they are. However, once mastered, these functions can be extremely useful. When used properly, they can speed up programs as well as save many many bytes of memory. When used in some BASIC programs, they can condense a multitude of IF...THENs into a single bit manipulation. You will find many uses for these functions and the time you spend learning them will be well worth it. --Craig Steiner -- Michael Current, Cleveland Free-Net 8-bit Atari SIGOp -->> go atari8 <<-- The Cleveland Free-Net Atari SIG is the Central Atari Information Network Internet: currentm@carleton.edu / UUCP: ...!umn-cs!ccnfld!currentm BITNET: currentm%carleton.edu@interbit / Cleveland Free-Net: aa700
- Next message by date: Michael Current: "SuperArc, Disk Communicator"
- Previous message by date: Michael Current: "TUTORIAL: CIOV"
----------------------------------------- Return to message index