We all know that all data that is stored in a computer is converted to sequences of 0s and 1s but how this system works let me tell you
A computer’s memory is divided into tiny storage locations known as bytes. One byte is only enough memory to store a letter of the alphabet or a small number. In order to do anything meaningful, a computer has to have lots of bytes. Most computers today have millions, or even billions, of bytes of memory.
Each byte is divided into eight smaller storage locations known as bits. The term bit stands for binary digit. Computer scientists usually think of bits as tiny switches that can
be either on or off. Bits aren’t actual “switches,” however, at least not in the conventional sense. In most computer systems, bits are tiny electrical components that can hold either a positive or a negative charge. Computer scientists think of a positive charge as a switch in the on position, and a negative charge as a switch in the off position. Figure 1-6 shows the way that a computer scientist might think of a byte of memory: as a collection of switches that are each flipped to either the on or off position
Think of a byte as eight switches
When a piece of data is stored in a byte, the computer sets the eight bits to an ON/
OFF pattern that represents the data. For example, the pattern on the left in Figure 1-7
shows how the number 77 would be stored in a byte, and the pattern on the right shows
how the letter A would be stored in a byte. We explain below how these patterns are
Bit patterns for the number 77 and the letter A
A bit can be used in a very limited way to represent numbers. Depending on whether the bit is turned on or off, it can represent one of two different values. In computer systems, a bit that is turned off represents the number 0, and a bit that is turned on represents the number 1. This corresponds perfectly to the binary numbering system. In the binary numbering system (or binary, as it is usually called), all numeric values are written as sequences of 0s and 1s. Here is an example of a number that is written in binary:
The position of each digit in a binary number has a value assigned to it. Starting with
the rightmost digit and moving left, the position values are 20, 21, 22, 23, and so forth, as
shown in Figure 1-8. Figure 1-9 shows the same diagram with the position values calculated. Starting with the rightmost digit and moving left, the position values are 1, 2, 4, 8, and so forth.
The values of binary digits as powers of 2
The values of binary digits
To determine the value of a binary number, you simply add up the position values of all the 1s. For example, in the binary number 10011101, the position values of the 1s are 1, 4, 8,16, and 128. This is shown in Figure 1-10. The sum of all of these position values is 157. So, the value of the binary number 10011101 is 157.
Determining the value of 10011101
Figure 1-11 shows how you can picture the number 157 stored in a byte of memory. Each 1 is represented by a bit in the on position, and each 0 is represented by a bit in the off position.
The bit pattern for 157
When all of the bits in a byte are set to 0 (turned off), then the value of the byte is 0. When all of the bits in a byte are set to 1 (turned on), then the byte holds the largest value that can be stored in it. The largest value that can be stored in a byte is 1 + 2 + 4 + 8 + 16 +32 + 64 + 128 = 255. This limit exists because there are only eight bits in a byte.
What if you need to store a number larger than 255? The answer is simple: use more than one byte. For example, suppose we put two bytes together. That gives us 16 bits. The position values of those 16 bits would be 20, 21, 22, 23, and so forth, up through 215. As shown in Figure 1-12, the maximum value that can be stored in two bytes is 65,535. If you need to store a number larger than this, then more bytes are necessary.
Two bytes used for a large number
TIP: In case you’re feeling overwhelmed by all this, relax! You will not have to actually convert numbers to binary while programming. Knowing that this process is taking
place inside the computer will help you as you learn, and in the long term this knowledge will make you a better programmer.
Any piece of data that is stored in a computer’s memory must be stored as a binary number. That includes characters, such as letters and punctuation marks. When a character is stored in memory, it is first converted to a numeric code. The numeric code is then stored in memory as a binary number.
Over the years, different coding schemes have been developed to represent characters in computer memory. Historically, the most important of these coding schemes is ASCII,
which stands for the American Standard Code for Information Interchange. ASCII is a set
of 128 numeric codes that represent the English letters, various punctuation marks, and
other characters. For example, the ASCII code for the uppercase letter A is 65. When you
type an uppercase A on your computer keyboard, the number 65 is stored in memory (as
a binary number, of course). This is shown in Figure below
The letter A is stored in memory as the number 65
TIP: The acronym ASCII is pronounced “askee.”
In case you are curious, the ASCII code for uppercase B is 66, for uppercase C is 67, and so forth. Appendix C shows all of the ASCII codes and the characters they represent.
The ASCII character set was developed in the early 1960s and was eventually adopted by
almost all computer manufacturers. ASCII is limited, however, because it defines codes for only 128 characters. To remedy this, the Unicode character set was developed in the early 1990s. Unicode is an extensive encoding scheme that is compatible with ASCII, but can also represent characters for many of the languages in the world. Today, Unicode is quickly becoming the standard character set used in the computer industry.
Advanced Number Storage
Earlier, you read about numbers and how they are stored in memory. While reading that
section, perhaps it occurred to you that the binary numbering system can be used to represent only integer numbers, beginning with 0. Negative numbers and real numbers (such as 3.14159) cannot be represented using the simple binary numbering technique we discussed.
Computers are able to store negative numbers and real numbers in memory, but to do so they use encoding schemes along with the binary numbering system. Negative numbers are encoded using a technique known as two’s complement, and real numbers are encoded in floating-point notation. You don’t need to know how these encoding schemes work, only that they are used to convert negative numbers and real numbers to binary format.
Other Types of Data
Computers are often referred to as digital devices. The term digital can be used to describe anything that uses binary numbers. Digital data is data that is stored in binary format, and a digital device is any device that works with binary data. In this section, we have discussed how numbers and characters are stored in binary, but computers also work with many other types of digital data.
For example, consider the pictures that you take with your digital camera. These images
are composed of tiny dots of color known as pixels. (The term pixel stands for picture
element.) As shown in Figure below, each pixel in an image is converted to a numeric code that represents the pixel’s color. The numeric code is stored in memory as a binary number.
A digital image is stored in binary format
The music that you stream from an online source, or play on an MP3 player is also digital.
A digital song is broken into small pieces known as samples. Each sample is converted to
a binary number, which can be stored in memory. The more samples that a song is divided into, the more it sounds like the original music when it is played back. For example, a CD quality song is divided into more than 44,000 samples per second!