Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How Important Are Binary Numbers To A Programmer?
You should absolutely learn binary. First, because it's really really easy. It's like the math you already know, but with fewer digits. Second, because binary and hexadecimal are an easy way to understand the native "size and shape" of numbers inside computers -- and *that* is important.

Doing binary math is so easy I'm going to show you how to do it right now. Do this math problem, just like you would if it was decimal....

PHP Code:


See, that was easy right? It looks the same in decimal and binary. The *difference* is in the value of the digits.

In decimal math, each place is worth 10 times more, so the "2" in "20" is worth 2x10, and in "200" is worth 2x10x10 = 2x100. In binary, each place is worth 2 times more.

So the 1 in "10" binary is worth 1x2 and in "100" is worth 1x2x2 = 1x4. What is the binary number "111"? From left-to- right, it's 1x2x2 + 1x2 + 1 = 7 in decimal.

That was easy right? Convert binary "101" and "010" and then add the result to convince yourself the above math makes sense in either binary or decimal.

I admit, the problem above was ultra simple, because there was no *carry*.
When doing decimal math, when you get past 9, you have to "carry the 1".
In binary, you only use the digits 0- and-1, so you have to carry when you get past *1*.

Let's go through a problem with carry step by step...
PHP Code:


• the rightmost column, 1+1 in binary is "10" or "0 carry the 1", so your right most column is "0 carry the 1"

• in the middle-column, you have the carry plus the two digits, so 1+0+1, which is "10" or "0 carry the 1" again

• in the leftmost column, you then have the carry plus the two digits, 1+0+0... which is just 1.

It may seem tricky, but it's just the same as doing decimal math. You just have to limit yourself to the digits 0 and 1.

Hexadecimal is also nice, because one digit always equals 4 bits of binary. The largest hexadecimal digit is F (sometimes written 0xF), and represents binary (1111). This allows you to see the "shape" of numbers in the computer intuitively.

Getting the full hang of binary and hexadecimal will take more time, but doing so has many benefits. It will help you intuitively understand the following questions, and more.

1. Why does 250 fit into 8 bits, 1 byte, but 260 (normally) requires more bits?

2. Why does storing sign reduce the range of numbers I can store in half?

3. How are negative numbers stored in a binary computer?

4. How does math with negative numbers work in a computer?

5. How does a computer store floating point numbers, and what causes "floating point precision" problems?

6. How do you "encode" data to fit into smaller numbers of bits?

7. Why is a 32bit CPU faster than an 8bit CPU? What exactly is it *doing* with those 32 bits of information?

8. How does SIMD/MMX work? How does being able to do 4 of the same exact operation simultaneously on 4 sets of values speed things up?

You want to understand those things, don't you? Here are some short answers. You’ll need to learn binary to understand them.

1. The largest number you can store in 8 bits (without an encoding) is 255, that is 1111 1111. Multiply out each digit by the proper power of two to see why.

2. One bit is used to store the sign information, which removes one power of two, and thus allows you to store numbers half as large. For example, the largest digit in an 8 bit byte is normally 2^8, but if you have a sign bit, it is 2^7.

3. In a crafty bit of engineering, we use the most significant bit to store the sign of a number, in a form we call 2-s complement . This means positive numbers are encoded normally, and negative numbers are encoded such that if you add them to positive numbers naturally, you get a 2-s complement encoded result without any extra work.

4. Because of 2-s complement, addition and subtraction ‘just work’. Multiplication and division require some additional work .

5. In a computer, everything must be encoded into bits, and that includes floating point numbers. The most popular encodings we used were standardized by IEEE, and involve using an exponent and mantissa. This mirrors scientific notation, where we write things like 6 x 10^9. This means they can represent small precise numbers (1.234 x 10^-3), or very large numbers (1.234 x 10^54), but not both at the same time. To understand why, imagine you just have 4 digits of mantissa (the first part). The number 1,234,567,891 is chopped into 1.234 x 10^9, which chopped off a bunch of digits, losing precision.

6. 8 bits can store exactly 255 distinct values, but there are no rules about exactly what those values mean. 2-s complement encoding changes the meaning of the bits to store -128 to 127, instead of 0–255. Floating point encoding splits bits into exponent and mantissa, allowing us to store an 8-bit floating point number, like 1.2 x 10^5. (8bit floating point does not have enough bits to be practically useful so we primarily use 16,32, or 64 bit floating point) Compression also encodes data into bits. One of the simplest form of compression is called Run Length Encoding (RLE). In a nutshell, if a digit (or pattern) is repeated, you record the digit and then the number of times it occurs, So instead of writing 10 occurances of 0, we might write “0 - 10”. To get back the original data, this must be decoded. Compression doesn’t get you something for nothing. In exchange for making certain streams of bits shorter, it makes other streams of bits longer.

7. Let’s only consider simple CPUs that do one operation per clock cycle. An 8bit CPU can add two 8 bit numbers per clock cycle, while a 32bit CPU can add two 32bit numbers per clock cycle. If you want to do 32bit math on an 8bit CPU, you need to use four clock cycles. Which makes a 32bit CPU roughly four times faster at doing 32bit or larger math.

8. SIMD stands for single instruction multiple data. Instead of an ADD instruction finishing one 32bit integer addition per clock cycle, it might finish four at the same time. We do this because 32bit and 64bit numbers are pretty big, so there is little need for us to move to 128bit or 256bit math. Instead, when we have a CPU operating on 128bits at a time, it can do more useful work if we split it into 4 32bit SIMD operations.

Quora question by David Jeske
Nothing Is Impossible
[-] The following 1 user Likes cheatex's post:
  • dealwap
Nice one, I remember writing a JavaScript program to convert from binary to decimal. I will share it here.
It takes so long to learn simplicity.
I wish I had concentrated on maths back then thou I wasn't thought quantitative reasoning in P School,
I am a proud member of NIgerian Coders Team
(12-27-2016, 09:14 PM)Voski Wrote:
I wish I had concentrated on maths back then thou I wasn't thought quantitative reasoning in P School,

its never too late bro.
It takes so long to learn simplicity.
(12-27-2016, 10:27 PM)Dealwap Wrote:
its never too late bro.

sure. but am finding it more difficult now, I take it as, if you don't know maths, then just forget it. programming ain't for you.
I am a proud member of NIgerian Coders Team
(12-27-2016, 10:40 PM)Voski Wrote:
sure. but am finding it more difficult now, I take it as, if you don't know maths, then just forget it. programming ain't for you.

Study some basic Mathematics and you're very good to go just know how numbers works.
It takes so long to learn simplicity.
(12-27-2016, 09:14 PM)Voski Wrote:
I wish I had concentrated on maths back then thou I wasn't thought quantitative reasoning in P School,

Hmmm. numbers are funny but it's helps id you can control and manipulate them. That's maths for you. Am not even good with it.
Nothing Is Impossible

Possibly Related Threads...
Thread Author Replies Views Last Post
  HELP: A Programmer Needs Help abcheats 16 382 04-12-2018, 06:43 PM
Last Post: abcheats
  SHARED: A Programmer, His Thoughts And Actions jonath45 6 169 01-25-2018, 08:05 AM
Last Post: Dynamo
  See Some Reasons Why Any Programmer Can't Make A Website Like Facebook? iamdannyfc 14 450 12-28-2017, 08:09 AM
Last Post: charliecyber
  SHARED: Top 7 Important Programming Concepts Horlahcoded 3 184 08-03-2017, 11:33 AM
Last Post: Aniexcels
  Programmers, Can You Marry A Programmer? dealwap 22 402 07-11-2017, 03:42 PM
Last Post: Pearstep
  FUN: Are You A Real Programmer? Click Here And Know The Truth wisetalent1 0 88 07-10-2017, 09:46 PM
Last Post: wisetalent1
  ALERT:  Who Is The Best Programmer In Nigeria As A Blogger wisetalent1 25 586 07-10-2017, 02:55 PM
Last Post: wisetalent1
  Facinating Story Of Yusuf - The Unschooled Programmer LightYagami 0 115 06-22-2017, 11:58 AM
Last Post: LightYagami
  How Do You Spot A Fake Programmer? dealwap 1 161 06-13-2017, 09:33 AM
Last Post: Horlahcoded
  Which Are You: A Programmer Or A Software Developer? cheatex 12 427 05-10-2017, 09:05 AM
Last Post: cheatex

Users browsing this thread: 1 Guest(s)

About Nigeria's Creative Talents

Nigeria's Creative Talents is an online community where every creative Nigerians can share ideas with themselves.

For any more information, please use our contact form.

              User Links