Colr Attrbt (And 15)

 BBS: Inland Empire Archive
Date: 07-31-92 (09:12)             Number: 171
From: JEAN CREPEAU                 Refer#: NONE
  To: BRYAN HOOVER                  Recvd: NO  
Subj: Colr Attrbt (And 15)           Conf: (2) Quik_Bas
In a message to ALL, BRYAN HOOVER wrote:
BH=> Can anyone explain how works the formula, ForColr% = Atribute% AND  15
    ? What's happening with the AND operator and the number 15?

        The  AND  operator  is  a  logical  operator,  like  OR and XOR. To
understand how it works, you must know  what is a binary number and how  to
convert a decimal (normal) number into binary or vice-versa.

        A binary number is a set of binary digits (bits). It's exactly like
a normal number which use 10 different digits. In binary, you can only  use
2  differents  digits:  0  and  1.  These two symbols represent an electric
state:  0V  and  5V.  You  computer  use  binary numbers 99.9% of the time.
Decimal numbers are too complicated.

        To convert a decimal number into binary, you divide that number  by
2. You keep the rest. You divide the result of the division by 2 again  and
keep the rest. You continue until you get 0. For instance, suppose you want
to convert 15:
        15/2 = 7 rest 1
        7/2  = 3 rest 1
        3/2  = 1 rest 1
        1/2  = 0 rest 1

        The binary number is 1111. 15=1111b! The last rest you get  becomes
the first digit in the binary sequence... my example wasn't very good!  Try
69.
        69/2 = 34 rest 1
        34/2 = 17 rest 0
        17/2 =  8 rest 1
         8/2 =  4 rest 0
         4/2 =  2 rest 0
         2/2 =  1 rest 0
         1/1 =  0 rest 1

        Then, 69=1000101b. It's easy isn't it?

        Now to convert you binary number  into a decimal number you do  the
opposite of what you did: you setup an accumulator in your mind.
        1. You multiply this accumulator by 2
        2. You add the value of the next digit in the sequence
        3. You repeat for any extra bit

        Ex: 110011b=?
        Acc.    Acc*2   Bit     Acc*2+Bit
        0       0       1       1
        1       2       1       3
        3       6       0       6
        6       12      0       12
        12      24      1       25
        25      50      1       51

        Then, 110011b is 51! The bits are generally numbered. The rightmost
one being 0, the preceding one being 1, etc... If you write the  mathematic
equation the algorithm above, you easily see that number=
        bit#0 + bit#1*2 + bit#2*2*2 + bit#3*2*2*2 + bit#4*2*2*2*2 + etc...
=       b0 + b1*2 + b2*4 + b3*8 + b4*16 + etc...

        A BYTE or character is a binary  number of 8 bits. It can take  the
values 00000000b to 11111111b (0 to 255). A WORD (or INTEGER in QBasic)  is
a binary number of 16 bits (0 to 65535). A DWORD (or LONG) is 32 bits wide.

        When  you  do  a  logical  AND,  OR  or XOR, you perform bit-by-bit
operation with the following truth table:
        x       y       AND     OR      XOR
        0       0       0       0       0
        0       1       0       1       1
        1       0       0       1       1
        1       1       1       1       0

Note that:
1. x AND 0 = 0, whatever is x
2. x AND 1 = x
3. x AND x = x
4. x OR 0 = x
5. x OR 1 = 1
6. x OR x = x
7. x XOR 0 = x
8. x XOR 1 = complement of x (0 becomes 1 and 1 becomes 0)
9. x XOR x = 0

        Often, the information is compressed into memory... 8 bits are  not
always necessary to  store a number.  For instance if  you want to  store a
color 0 to 15, you  only need 4 bits. Why  use 8 bits? you would  loose the
other 4 bits,  unless you want  to store 256  different numbers (0  to 255)
instead of 16. What we usually do is that we allocate functions to specific
bits.  For  instance,  in  a  graphic  card,  the  attribute BYTE read with
PEEK(adress) counts 8 bits:
        b0 to b3: Foreground (4 bits = 16 different colors 0000-1111)
        b4 to b6: Background (3 bits = 8 colors 000-111)
        b7: Blink (1 bit = 0 or 1)

        If you want to read only the foreground for instance, you must have
a way to 'ignore' bits b4 to b6. This is done by law #1:

        b7 b6 b5 b4 b3 b2 b1 b0         Attr
AND      0  0  0  0  1  1  1  1           15
        -- -- -- -- -- -- -- --         ----
         0  0  0  0 b3 b2 b1 b0           FG

        If you want the background,
        b7 b6 b5 b4 b3 b2 b1 b0         Attr
AND      0  1  1  1  0  0  0  0          112
        -- -- -- -- -- -- -- --         ----
         0 b6 b5 b4  0  0  0  0           BG*16

        Then (Attr AND 112)/16 will give you the background color 0-7.

        OR is use  if you want  to set some  bits and XOR  when you want to
invert some bits.

        Hope this will help you to understand... Binary is not that easy to
understand when you never heard about it... I know.
                Jean
---
 * Origin: INTERACESS Montreal (QC) Canada (514) 528-1415 (1:167/280)
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