Whogohost Advert On NCT|
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How Important Are Binary Numbers To A Programmer?
#1
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:
101 
010 

-------
111 


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:
001 
011 

------- 
100 


• 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
#2
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.
Reply
#3
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
Reply
#4
(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.
Reply
#5
(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
Reply
#6
(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.
[-] The following 1 user Likes Dealwap's post:
  • Voski
Reply
#7
(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
[-] The following 1 user Likes cheatex's post:
  • Voski
Reply
Facebook Share


Possibly Related Threads...
Thread
  ALERT:  Top 19 Reasons Why You Should Date A Programmer
  Things To Know And Be Aware Of Before Dating A Programmer - Shared From Experience!
  6 Signs That You Are Not Meant To Be A Programmer
  Complicated Terms: The Difference Between A Programmer, Coder, Developer And Engineer
  How To Make Money From Open Source Software/Project As A Programmer
  The Myth Of The Super Programmer
  NCT Forum Programmer: Don’t Just Code, Solve Problems!




Users browsing this thread: 1 Guest(s)