Lesson 1: Transforming Numerical Systems


E-mail this post



Remember me (?)



All personal information that you provide here will be governed by the Privacy Policy of Blogger.com. More...



This is the first lesson I wrote. Its purpose is to introduce you to the world of programming, showing you the way computers think or archive data. Please be patient and read through the whole lesson carefully in order to fully understand showed examples and calculations. I understand you can't wait to start using the code, but believe me these general foundations are somehow necessary to fully understand next tutorials and lessons I'll provide you with. Take time, new lessons will be published in a time interval necessary for me to compose them for your, and in the mean time you have time to carefully run through previous like this one. Now we can begin...


Binary Numbers

Base system uses base B, and includes numbers 0, 1, 2, ... , B‑1

For example in decimal system B=10, and numbers included are 0, 1, 2, ..., 8 & 9.

If the base B=2, then the system is binary, and its numbers are 0 & 1.

From English term BInary digiT, comes the name for lowest amount of information BIT.

Example:

5710 = 5 * 101 + 7 * 100 = 1*25 + 1*24 + 1*23 + 0*22 + 0*21 + 1*20 = 1 1 1 0 0 1 2

Number 57 in decimal is shown by two numbers, while in binaries six figures are necessary to show data. Binaries compared to other numbering systems, logically use most amount of elements (data) to give us information. Number of BITS used to write down a number is limited due to technical reasons.

Machines used to process and save binary information, are usually constructed form electronic elements with 2 stable states (bistables), which are efficient and cheap to produce.

Switching from decimal to binary numbers:

Binary number is made by leftovers we get from dividing (reading upwards)

57 : 2 = 28 1 1 1 0 0 1

1

28 : 2 = 14

0

14 : 2 = 7

0

7 : 2 = 3

1

3 : 2 = 1

1

1 : 2 = 0

1



Negative Binary Numbers

Operation 7 – 5 , using computer with 4 bit length registry will be processed like

7 + (-5). Binary result of -5 can be achieved:

Positive Number 0 1 0 1

Complement till base -1 1 1 1 1

( one komplement) - 0 1 0 1

1 0 1 0


Complement till base 1 0 1 0

(dual complement) + 0 0 0 1

1 0 1 1



Proof that the result is - 5 Excluding operation 7 - 5

1 0 1 1 (-5)

+ 0 1 0 1 (+5)
---------------
0 0 0 0

extra 1



0 1 1 1 ( 7)

+ 1 0 1 1 (-5)
---------------
0 0 1 0

extra 1

If we have n=3 bit length registry, (first bit reserved for telling us if its –/+), next numbers can be shown:

Decades Binaries
0 000
1 001
2 010
3 011

-4 100
-3 101
-2 110
-1 111


For n = 3 we get interval [‑22, 22 ‑ 1], in general [‑2n‑1, 2n‑1 ‑ 1].

For n = 8 we get interval [‑27, 27‑1], exact range [‑128, 127].

Octal System

Base of the system is B=8 and numbers used are 0, 1, 2, 3, 4, 5, 6, 7.

This system is used for shorter description of binaries when its needed.

Example:

36‑bit number 001 110 000 101 111 001 010 011 111 000 100 001

Octal equivalent 1 6 0 5 7 1 2 3 7 0 4 1

Hexadecimal System

Base of the system is B=16 and numbers used are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. This system is used for shorter description of binaries when it’s needed.

Example:

16‑bit number 0111 1011 0011 1110

Hexadecimal equivalent. 7 B 3 E


Rational numbers

Rational numbers have binary “spot” which is similar to decimal spot used by decimal numbers.

Example: conversion of rational number

5.75 10 = 5 * 100 + 7 * 10‑1 + 5 * 10‑2 =

= 1*22 + 0*21 + 1*20 + 1*2‑1 + 1*2‑2 = 1 0 1 . 1 1 2

Conversion of decimal number to binary number

1.25 = 1 + .25

.25 * 2 1 . 0 1

0.50

.5 * 2

1.0


Example: conversion of this decimal number - when conversed, it has infinite number of fractions in its binary state

13.3 = 13 + 0.3

.3 * 2 1 1 0 1 . 0 1 0 0 1 1 0 0 1 ...
0.6

.6 * 2
1.2

.2 * 2
0.4

.4 * 2
0.8

.8 * 2
1.6

.6 * 2
1.2

....


Notice that finite decimal number is shown as infinite periodic binary number.

Binary Number is mounted with exponent of Base 2, in a way that a binary dot is moved left or right, depending if exponent is positive or negative

Example: 1 . 1 1 * 22 = 1 1 1



Update - March, 8. 2006: Don't panic! if you didn't quite understand these numerical systems, You can still read my additional lesson that should in-depth cover this topic. There are many examples on numeric conversions also. Soon, I'm going to post another lesson on calculations with binarie numbers.




Technorati Tags: , , , , , ,



27 Responses to “Lesson 1: Transforming Numerical Systems”

  1. Anonymous Spaceman 

    is there gonna be some tutorials in visual c++?

  2. Anonymous Wasim 

    I constanty check ur LordofNet blog. Nice work

  3. Anonymous vurdlak 

    thank you both. there is gonna be tutorials every few days. first topic im covering is intro to computer language foundations, then programming in c (getting used to a code) then algorithms and structure of data, (lysts, files, etc. with c) then c++ tutorials... ...

    in shot you'll learn from zero to making advanced programs yourelf!

    this is great opportunity for all of you to follow my work and learn with almost no effort just by reding every few days this site, but I'll also include some homework for you guys, and already included some nice examples in tutorials

  4. Anonymous NrmMyth 

    Nice try. Keep up the good work.

  5. Anonymous Z 

    Good stuff, a little confusing but good stuff. ^ ^

  6. Anonymous Chris 

    Shouldn't a set of tutorials on C start with lesson 0? :-)

  7. Anonymous vurdlak 

    If you wan't to get technical, yes. But in that case I think I would put it:

    0
    01
    10
    11
    100
    101
    110
    111
    ...
    ..
    .

    and so on for every following lesson ;)

  8. Anonymous jc-denton 

    Most c Tutorials start with HelloWorld and introduce some Keywords in C.. You start first with the basics (like this capter). I think this approach is much better since the reader will understand better how his/her computer works.

    Thanks for writing this tutorial and keep up the good work!

  9. Anonymous stipa 

    sorry, but this tutorial is only understandable until negative binary numbers - you did not add any comment to help a newbie understand it (complement till base -
    what's a 'base'?, there's no explanation). if you want your tutorials to be helpful, you need to be more informative. i apperciate the effort, but i doubt that someone who didn't use binary before, will learn it from this lesson.

  10. Anonymous vurdlak 

    look at the beginning of the article and if you read it you will see Base explanation.

  11. Anonymous nickeax 

    Cool! Thanks for taking the time to provide such a useful resource. I hope many people use it.

  12. Anonymous naters 

    i understand positive binary numbers (i have a degree in networking) but you lost me at negative binary numbers. if you could further explain this i would be grateful. i'm currently pursuing a degree in web programming and would like to gain some understanding of the more powerful, lower-level, C language.

  13. Anonymous fuzzyping 

    I have to agree with naters and stipa (partially). You lost me at negative binaries; what is a complement till base? The rest of the tutorial was mildly informative, probably not for n00bs.

  14. Anonymous Greg 

    I have no understanding of programing. I'm fairly proficient in using and repairing computers. I'm just wondering, am I out of my league in trying to understand this stuff or is there some prerequisite you recomend for me?

  15. Anonymous Luke 

    Without reading any of the other lessons, I'm just curious why you would want to start out with binary number. The whole purpose of using a high-level programming language is to abstract away having to know a thing about binary.

  16. Anonymous Chris 

    After being out of high school math for 27 yrs, I got left behind at the first paragraph. Can anyone suggest a pre-tutorial that covers binaries, etc. more thoroughly?

  17. Anonymous Mike 

    In response to Luke, if one is to properly use C one must understand how it stores data. The power and efficiency of C derives in part from its ability to directly access memory, which in effect makes it very dangerous (e.g. buffer overflows). There is no protection against improper memory access, and there is no garbage collection. C is not as high level as most other languages used today, and requires more attention to technical detail. I could go on for ever about this, but it's not my tutorial. That being said, I think verdlak is doing a great job.

  18. Anonymous Bhuvan 

    Negative Binary Numbers part is very Confusing.

  19. Anonymous Ada 

    so which method do the computers (let's say intel based) use to store the negative numbers? sign & magnitude or 2's compliment?

  20. Anonymous vurdlak 

    Hy,

    I'll post add-on soon to this lesson explaining more on computer storage of binary numbers. This lesson is written in order for you to get a feeling how computer reads and stores data in bits, but isn't required to understand in depth. Anyhow I'll work on follow-up to this lesson where I'll go little offtopic lecturing you more on binary data and computer storage, and post it here in few days.

    Answer to Ada:

    Computer stores numbers in NBC, 1' complenet, 2' complenet, BCD... or some other variant, but 2' complement is most spread way of stgoring positive and negative numbers which ALU (arithmetic-logical unit) can understand.

    Please allow me few days to compose an article where I'll publish more detailed description of NBC (most basic storage of positive numbers), 1' complement, 2'complement, BCD, and operations done used previously mentioned methods of storing data.

  21. Anonymous vurdlak 

    Update - March, 8. 2006: Don't panic! if you didn't quite understand these numerical systems here, You can read through my additional lesson that in-depth covers them all; conversions also. Soon, I'm going to post another lesson on calculations with binarie numbers.

  22. Anonymous parthpatel 

    when shall u put something more interesting then these highschool programming ? cant u put bit profesional?

  23. Anonymous confuzzled? 

    i have no idea what the negative binary numbers are doing haha, i get everything else, but the negative binary

  24. Anonymous zman 

    Somewhat different approach to start a C tutorial but looks good, keep up the good work!

  25. Anonymous bsd 

    good material! the presentation is good as starting material

  26. Anonymous ns 

    good job......one quick question....how can I get compilers? and can I ask questions to clear my doubts?

  27. Anonymous cply02 

    I want to study programming c
    you guid me about it

Leave a Reply

      Convert to boldConvert to italicConvert to link

 



Daily Lessons for programming in Visual Studio, using C code.