If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

### Course: Code.org>Unit 1

Lesson 1: How computers work

# Binary & data

Computers use bits (binary digits) to represent data as ones and zeroes. Bits are the smallest piece of information a computer can store. Explore how computers use the binary number system to represent numbers, text, images, and sound with electrical signals in their circuits.

## Want to join the conversation?

• Is Binary 0 and 1 in Computers like the DNA for Humans except computers use Binary to function the data and information?
• Binary is more like the smallest building blocks of information (just a 1 or 0). Its how they are strung together that gives the computer instructions and may be closer to DNA since DNA gives our bodies instructions. Binary by it self seems to be closer to atoms (Building blocks).
Algorithms may also be compared to DNA since they are instructions that tells the computer what to do with the input in order to give output. Really there is no perfect comparison, life and nature are still much much more sophisticated and complicated than even the most advanced computer...so far ;-)
• 255 is the maximum rgb vslue in JavaScript. Why is that?
• with three numbers (rrr, ggg, bbb) you can describe 16 777 216, isn't it enough?
Anyway, human eye can distinguish about 10 millions colors (10 000 000), so being able to use more numbers to describe colors would be a wasteful use of memory.
Using one less wire would mean having 127 as the max number, and so having 2 097 152 colors, which is not enough
• Is it possible instead of making a computer with switches being either 'on' or 'off', can you make a 'three-way switch' that has 3 different settings like 0, 1, and 2?
Even better, can computers use `hexadecimal` instead of binary? Wouldn't it be much more efficient to store information like that?
• Computers process the binary data via electrical pulses. To represent a 0, we have no pulse, and a 1, we a pulse. If we wanted to have more numbers added, we would use different magnitudes of pulses. However, the transistors are so good they can't quite detect magnitudes of pulses that well, but really can only detect the presence of one. To make it easier to detect the magnitudes, we could have the voltage be higher. But, if the voltage is higher, so is the energy consumption of the computer. For this reason, computers just stick to the simplest number system they can, binary.
• if text and photo and video converts to binary, that means everything become binary, so how computer know which is text and which are photo ?
• From the author:Good question! We discuss that more here:

Basically, every file has a "header" at the top, and that header provides information about the contents of the file, like it's file type.

An application can look at a file header to determine if it understands the file type and can open it. For example, a text editing application can't open photos, so it will look at the file header, see that it's a type that it doesn't understand, and then tell the user "sorry, can't open that!". A photo editing application will look at the file header, see that it's an image type that it understands, and then render the pixels for the image.
• From of this lecture(i.e. Text in Binary), we learned that we can assign a number to each letter (The ASCII and Unicode System).
Now one question is troubling me a lot, In ASCII letter 'A' is represented by Decimal 65 and in Binary it will be 01000001 and this binary 65 (01000001) will be stored as a letter 'A' in computers memory, to be outputted by the monitor.

But at the same time the same binary (01000001) means a numeric 65.

How does CPU know when to represent (01000001) as letter 'A' and when the number 65?

Please make it clear to me and I shall be very much thankful to you.
• Those bits can be letters, colors, numbers, pictures, or just about anything else you can think of. It is our choice on how we interpret the data.

Often, for data files, we put some special string of bytes at the beginning of the file (a header), and also we add a file name extension e.g. .pdf, .jpg, etc. to give programs a clue on how we want them to interpret the data. However, those programs can choose to interpet that data in any way that they want e.g. you could try to display a text file as a picture (it would probably just look like a bunch of random pixels)
• After input is given to computer , it will store in the form of 0's and 1'S . Before displaying output how the computer knows that particular combination of binary digits related to image or text or video?
• Often, files containing binary data will have a "file signature" at the front of the file which gives a clue to what the data is. For example for a PNG image file the first 8 bytes are:
The first eight bytes of a PNG file always contain the following values:
(decimal) 137 80 78 71 13 10 26 10
(hexadecimal) 89 50 4e 47 0d 0a 1a 0a
(ASCII C notation) \211 P N G \r \n \032 \n

So an image program could look at a file with this signature, and know that is probably a PNG file. However, you could still open that file up in a text editor and display it as text (most of it, other than the file signature would look probably look like random text), or you could try to play it as an audio file (it would probably sound like static).

Some programs, will just give you an error message, if it looks for a file signature and doesn't find anything that it recognizes.
• They say how to read binary, but how exactly does somebody write this? lets say I wanted to represent the number 44? How would I do that.
• Well it's just like writing normal base 10 numbers, but with different numbers that go into each place. Let's look at what happens when you write 44 normally:

1) First, you decide how many digits your number has by looking at whether the largest value is less than a certain place. Here, 44 is less than 10^2 or 100, so we know the number has 2 digits.
2) From there, you write the greatest place value down that's less than the value of the number. Here we would be looking at the tens place, and we put a 4 there because 5 would be 50, which is greater than 44
3) Now, we can take whatever's left (4) and assign it to the ones place. Technically, this is like you're dividing the original number (44) by the tens place (10), and seeing what comes out as a remainder.

For binary (base 2), it's pretty much the same:
1) Instead of looking at a power of 10, we look at a power of 2. 44 is bigger than 32 (2^5) but smaller than 64 (2^6), so our number has 6 digits. So far our number is ------
2) The greatest place would be a 1 in the 32-place. So so far our number is 1-----
3) We then divide by 2 (instead of 10). 44 / 32 gives a remainder of 12. This means we now write down what 12 is.
4) 12 is bigger than 8 (2^3) but smaller than 16 (2^4), so it needs 4 places. This means that the 5th place will have to be a 0. So far our number is 10----
5) The 8-place will be a 1 because it's the greatest that we're working with. So far our number is 101---
6) Then if you divide 12 by 8, you get a remainder of 4, which is our next number to consider. 4 is exactly 2^2, so honestly we can stop here because if 2^2 = 4, then we don't need any contribution from the 2^1 or 2^0 places.
7) This makes our final number 101100