How do computers store numbers? – J004

DeegeU Course

This video “How do computers store numbers?” is part of a larger free online class called “Free Java Course Online”. You can find more information about this class on “Free Java Course Online” syllabus.

Try at home

Try converting a few numbers to binary

Try converting a few numbers to hexadecimal

Transcript – How do computers store numbers?

We know computers are great with numbers …usually

But how does a computer store numbers. Some of you might answer we use zeros and ones. That’s right! But how those zeros and ones organized? Ah ha! How do we make numbers out of the zeros and ones? That’s what this lessons about.

The first type of number we can store in Java are the counting numbers. These should be the numbers that your most familiar with.

One, two, three etc. These numbers also include the number zero and negative numbers. The counting numbers are sometimes called the set integer numbers. These numbers go on for ever in both positive and negative direction. On a computer you can’t represent every number no matter how you construct the number.

Eventually you’re gonna run outta space on a computer. The set of valid numbers that you can use is limited in something we call a Java primitive. The reason for this is simple. In a computer a bit is used to store a value of either a zero or a one. This is the smallest piece of information. If you want bigger pieces of information, you need to string more bits together.

Your first thought might be, string more ones together and just count them. So for twelve, that’s a lot of ones just to represent twelve. Imagine if you wanted a bigger number like 42… or a million. That plan’s not going to work.

Think about how you represent decimal numbers based on powers of tens. A decimal number increases by powers of ten for each decimal place. So 123, equals one times a hundred plus, two times ten, plus three times one. This is really one times ten to the power of two, plus two times ten to the power of one, plus three times ten to the power zero.

We represent binary numbers as powers of two

Now it might not look like it, but this is the decimal system that you learned when you were a kid. We do the same thing for binary numbers in a computer. Like a decimal place we have binary places based on powers of two. So to represent twelve in binary, we have one times eight plus one times four, plus zero times two, plus zero times 1. Eight, four, two, and one are all powers of two. So the same way we represent decimal numbers, you can write twelve as 1100, in binary.

Even binary numbers organized this way can get crazy. As you can see, we needed four digits to represent twelve. As numbers get large in binary, we typically switch to a secondary number system called hexadecimal.

This number system is based on powers of sixteen. And we use letters for the digits beyond 9. You’re probably wondering why sixteen? It’s not a random number. Each hexadecimal digit is really a block of four binary digits. So binary 1010, is hexadecimal A, or ten in decimal. Pretty cool, huh?

Hey thanks for watching the video! There’s a quick quiz for this on DeegeU.com if you’d like to gauge how much you learned.

If you like the videos are seeing, please let me know by liking the video and hitting the subscribe button to the DeegeU channel on YouTube. I’d really appreciate that!

If you have concerns or questions please leave them in the comments below or on DeegeU.com.

There’s a poll on the front page in DeegeU.com, so you can let me know what topic is covered next.

Thanks for watching and see you in the next video!



Tools Used

Java

NetBeans



Media Credits

All media created and owned by DJ Spiess unless listed below.

No infringement intended

Get the code

The source code for “How do computers store numbers?” can be found on Github. If you have Git installed on your system, you can clone the repository by issuing the following command:

Don’t miss another video!

New videos come out every week. Make sure you subscribe!

Comments

comments

DJ Spiess

Your personal instructor

My name is DJ Spiess and I’m a developer with a Masters degree in Computer Science working in Colorado, USA. I primarily work with Java server applications. I started programming as a kid in the 1980s, and I’ve programmed professionally since 1996. My main focus are REST APIs, large-scale data, and mobile development. The last six years I’ve worked on large National Science Foundation projects. You can read more about my development experience on my LinkedIn account.