BITS AND BYTES

Having established what a file is, it is necessary (and easy) to understand the concept of a bit. You no doubt understand it already, but, just to recap, a bit is a single part of an electronic memory which can be set to one of two states: either to logic 1 ("on" - charged to a voltage which is usually, but not always, the same as the positive power rail that supplies the circuit, e.g. +5V); or to logic 0 ("off" - discharged to a zero voltage). Logic 1 and logic 0 are often referred to as high and low, respectively.

A memory IC can have any number of bits contained within it. A fixed number of bits is known as a byte. Typically a byte consists of 8-bits (in fact, this is the modern dictionary definition), but historically this was not the case. In the tutorials, we will refer to a byte as having 8-bits. The common term for a number consisting of more than 8-bits is the word "word". For example, the program memory of a PIC16F84 consists of 1024 instructions, each one is a 14-bit word.

The status of the bits (high or low) within a byte is expressed as a binary number reading from left to right, in order of the bit representing the highest value first (most significant bit - MSB) down to the lowest value (least significant bit - LSB).

Thus decimal 128 is expressed as binary 10000000, whereas decimal 1 may be expressed as binary 00000001 (the preceding number of zeros may not always be included - their presence being implied).

The bits of a byte or word are referred to by their position within it, with position 0 at the right, ascending as high as necessary depending on the byte/word length. Thus an 8-bit byte has its bits numbered as 7, 6, 5, 4, 3, 2, 1, 0. The use of 0 (zero) as a bit number is essential to remember when programming PICmicro's.

You will have seen that the LEDs and most switches are intentionally numbered in this reverse order. Incidentally, terms MSB and LSB can also be used to mean Most Significant Byte and Least Significant Byte. The appropriate meaning should be clear from the context. There are also similar terms, NMSB and NLSB, in which the N stands for Next.

We express numbers in binary when we wish to show the status (1 or 0) of each particular bit. Another more convenient way of expressing numbers is to use base 16, i.e. hexadecimal (or simply hex). In hex, numbers from 10 to 15 are represented by the letters 'A' to 'F' respectively. The decimal number '9' is written as '9' in hex, but the decimal '10' is 'A' in hex. Similarly, '11' is 'B' and so on up to '15' which is 'F'. The decimal '16' is then written as '10' in hex.

Why is this convenient? A byte which consists of 8-bits can be neatly represented by 2 hexadecimal symbols (each one is called a 'nibble' - we will discuss this in a later tutorial).

Now use the contents tree to navigate to Tutorial 2.4.