 # Bases

We are most familiar with numbers expressed in terms of powers of ten, or decimal numbers. Sometimes it is more convenient to give numbers in a program in another base. BASIC allows numbers to be given in hexadecimal (base 16) and binary (base 2) as well as base 10.

The computer treats any number which is preceded by an & sign as a hexadecimal (hex) number.

Whereas decimal numbers can contain ten separate digits, from 0 to 9, hexadecimal numbers can contain sixteen separate digits, 0 to 9 and A to F. The first 16 hexadecimal numbers and their decimal equivalents are given below:

Hex Decimal Hex Decimal
&0 0 &8 8
&1 1 &9 9
&2 2 &A 10
&3 3 &B 11
&4 4 &C 12
&5 5 &D 13
&6 6 &E 14
&7 7 &F 15

The next hexadecimal number is &10 which is equivalent to 16 in decimal notation. Thus, in hexadecimal notation, one in a column represents a power of sixteen rather than a power of ten. For example, &100 represents 256 which is 162.

#### Binary numbers and bits

You can enter numbers in binary notation, i.e. in base 2, by preceding them with the percent sign %.

Binary numbers consist entirely of the digits 0 and 1. The following table gives the binary equivalents of the decimal values 1 to 10.

Binary Decimal Binary Decimal
%1 1 %110 6
%10 2 %111 7
%11 3 %1000 8
%100 4 %1001 9
%101 5 %1010 10

A one in a particular column represents a power of two:

 27 26 25 24 23 22 21 20 128 64 32 16 8 4 2 1

Thus:

%1000101 = 1*64 + 0*32 + 0*16 + 0*8 + 1*4 + 0*2 + 1*1 = 69

Binary digits are usually referred to as bits.

#### Shift operators

There are three operators which act upon the 32 bits of an integer, shifting it either left or right by a given number of places.

##### Shift left

The simplest shift is <<. This shifts the bits of an integer to the left by a given number of places and inserts zeros in the righthand bits. For example:

```A% = 10
B% = A% << 1
C% = A% << 2
D% = A% << 3```

This leaves the variables with the following values:

Variable Value
A% 10 (%00000000000000000000000000001010)
B% 20 (%00000000000000000000000000010100)
C% 40 (%00000000000000000000000000101000)
D% 80 (%00000000000000000000000001010000)
##### Shift right (unsigned)

The >>> operator shifts the bits of an integer to the right a given number of times, losing the bits which were in those positions and introducing zeros at the left. For example:

```A% = %1010
B% = A% >>> 1
C% = A% >>> 2
D% = A% >>> 3```

This leaves the variables with the following values:

Variable Value
A% 10 (%00000000000000000000000000001010)
B% 5 (%00000000000000000000000000000101)
C% 2 (%00000000000000000000000000000010)
D% 1 (%00000000000000000000000000000001)
##### Shift right (signed)

The >> operator is similar to >>>, but instead of introducing zeros at the top at each stage, the left-most bit is set to either one or zero depending on what the current setting is. The left-most bit of an integer is normally used to indicate whether the integer is positive (left-most bit = zero) or negative (left-most bit = one). Consequently, this operator can be used to perform a division by a power of two on a number, retaining its sign. For example:

```A  = -1610612740
B  = 536870912```
```A% = %10100000000000000000000000000000
B% = %00100000000000000000000000000000
C% = A% >>> 2
D% = B% >>> 2
E% = A% >> 2
F% = B% >> 2```

This leaves the variables with the following binary values:

Variable Value
C% %001010000000000000000000000000000 (671088640)
D% %000010000000000000000000000000000 (134217728)
E% %111010000000000000000000000000000 (-402653184)
F% %000010000000000000000000000000000 (134217728)
##### Left shift as multiplication

The left shift operator can perform multiplication. The expression val<<n is equivalent to val * 2^n. So fred<<3 is the same as fred*8. Although using shift can be faster than the equivalent multiply, you should bear in mind that bits may be shifted off the end of the number, so leading to incorrect results which will not be trapped as errors. For example, &10000<<16 yields 0, whereas the correct 'multiply' result is &100000000 (which cannot be represented in a 32-bit integer, and would be converted to floating point by BASIC).

##### Right shift as division

The two right shift operators perform a similar role in division. The >> operator gives division of 'signed' numbers by a power of two. This means that both positive and negative numbers may be divided; the result is always rounded towards the integer less than or equal to the exact value. For example, -3>>1 is the same as INT(-3/2)=-2, not -3 DIV 2, which is -1. The >>> operator ignores the sign when shifting negative numbers, so should only be used to divide positive numbers by a power of two.

#### AND, OR and EOR

The operators AND, OR and EOR produce a result which depends upon the bits of two integer operands:

• In the case of AND, the bits in the two integers are compared and if they are both one, then a one is placed in the corresponding bit of the result.
• In the case of OR, a one is placed in the corresponding bit of the result if either or both of the bits in the integers are one.
• In the case of EOR, a one is placed in the corresponding bit of the result if either (but not both) of the bits in the integers is one.
Inputs AND OR EOR
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0

For example:

```A% = %1010
B% = %1100
C% = A% AND B%
D% = A% OR B%
E% = A% EOR B%```

This leaves the variables with the following values:

Variable Value
A% 10 (%1010)
B% 12 (%1100)
C% 8 (%1000)
D% 14 (%1110)
E% 6 (%0110)

The logical operators AND, OR and EOR are symmetrical, like + and *. Thus X AND Y = Y AND X for all possible values of X and Y. This applies to the other two operators as well.

#### TRUE and FALSE

The truth values TRUE and FALSE have the values -1 and 0 respectively. This means that:

##### With AND
```TRUE AND TRUE   gives TRUE  (-1 AND -1 = -1)
TRUE AND FALSE  gives FALSE (-1 AND  0 =  0)
FALSE AND FALSE gives FALSE ( 0 AND  0 =  0)```
##### With OR
```TRUE OR TRUE    gives TRUE  (-1 OR -1 = -1)
TRUE OR FALSE   gives TRUE  (-1 OR  0 = -1)
FALSE OR FALSE  gives FALSE ( 0 OR  0 =  0)```
##### With EOR
```TRUE EOR TRUE   gives FALSE (-1 EOR -1 =  0)
TRUE EOR FALSE  gives TRUE  (-1 EOR  0 = -1)
FALSE EOR FALSE gives FALSE ( 0 EOR  0 =  0)```