You are on page 1of 7

Lesson 1: Transforming Numerical Systems - C Tutorial

Lesson 1: Transforming Numerical Systems

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 <!--[if !vml]--><!--[endif]--> <!--[if !vml]--><!--[endif]--> <!--[if !vml]--><!--[endif]--> <!--[if !vml]--><!--[endif]--> <!--[if !vml]--><!--[endif]--> <!--[if !vml]--><!--[endif]--> <!--[if

!vml]--><!--[endif]--> <!--[if 28 : 2 = 14 <!--[if !vml]--><!--[endif]--> 14 : 2 = 7 <!--[if !vml]--><!--[endif]--> 7 : 2 = 3 <!--[if !vml]--><!--[endif]--> 3 : 2 = 1 <!--[if !vml]--><!--[endif]--> 1 : 2 = 0 <!--[if !vml]--><!--[endif]--> Negative Binary Numbers

!vml]--><!--[endif]--> 1 0 0 1 1 1

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 1 2 3 -4 -3 -2 -1

000 001 010 011 100 101 110 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 its 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: C++, Programming, Visual Studio, Octal, Binary, Hexadecimal, Conversion

27 Responses to Lesson 1: Transforming Numerical Systems Spaceman on 12:52 PM is there gonna be some tutorials in visual c++? Wasim on 1:47 PM I constanty check ur LordofNet blog. Nice work vurdlak on 2:29 PM 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 NrmMyth on 9:30 PM Nice try. Keep up the good work. Z on 8:53 PM Good stuff, a little confusing but good stuff. ^ ^ Chris on 1:15 AM

Shouldn't a set of tutorials on C start with lesson 0? :-) vurdlak on 1:31 AM 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 ;) jc-denton on 1:34 PM 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! stipa on 11:33 PM 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. vurdlak on 11:44 PM look at the beginning of the article and if you read it you will see Base explanation. nickeax on 7:39 AM Cool! Thanks for taking the time to provide such a useful resource. I hope many people use it. naters on 5:01 AM 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. fuzzyping on 7:04 AM 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. Greg on 5:10 PM 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? Luke on 8:21 PM 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. Chris on 1:17 PM 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?

Mike on 2:36 PM 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. Bhuvan on 9:29 AM Negative Binary Numbers part is very Confusing. Ada on 10:28 AM so which method do the computers (let's say intel based) use to store the negative numbers? sign & magnitude or 2's compliment? vurdlak on 7:46 PM 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. vurdlak on 3:27 AM 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. parthpatel on 2:12 PM when shall u put something more interesting then these highschool programming ? cant u put bit profesional? confuzzled? on 5:54 AM i have no idea what the negative binary numbers are doing haha, i get everything else, but the negative binary zman on 2:49 PM Somewhat different approach to start a C tutorial but looks good, keep up the good work! bsd on 7:24 AM good material! the presentation is good as starting material ns on 11:57 PM good job......one quick question....how can I get compilers? and can I ask questions to clear my doubts? cply02 on 8:38 AM I want to study programming c you guid me about itPosting your comment... Leave a Reply

Name (required) Website Convert to boldConvert to italicConvert to link Remember me

This Website is optimized for Firefox. Users browsing with Internet Explorer may encounter problems while viewing pages.

You might also like