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...

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

.25 * 2 1 . 0 1

0.50

.5 * 2

1.0

.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

....

Technorati Tags: C++, Programming, Visual Studio, Octal, Binary, Hexadecimal, Conversion

**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 **BI**nary digi**T, **comes the name for lowest amount of information **BIT**.

Example:

**57**_{10} = **5** * 10^{1} + **7 *** 10^{0} = **1***2^{5} + **1***2^{4} + **1***2^{3} + **0***2^{2} + **0***2^{1} + **1***2^{0} = **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 [‑2^{2}, 2^{2} ‑ 1], in general [‑2^{n‑1}, 2^{n‑1} ‑ 1].

For n = 8 we get interval [‑2^{7}, 2^{7}‑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** * 10^{0} + **7** * 10^{‑1} + **5** * 10^{‑2 } =

= **1***2^{2} + **0***2^{1} + **1***2^{0} + **1***2^{‑1} + **1***2^{‑2} = **1 0 1 . 1 1** _{2}

**Conversion of decimal number to binary number**

.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

.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 ** * 2^{2} = **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

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

- Lesson 1
- Lesson 2
- Lesson 3
- Lesson 4
- Lesson 5
- Lesson 6
- Lesson 7
- Lesson 8
- Lesson 9
- Lesson 10
- Lesson 11
- Lesson 12
- Lesson 13
- Lesson 14
- Lesson 15
- Lesson 16
- Lesson 17
- Lesson 18
- Lesson 19

- Binary To Decimal
- Decimal To Binary
- Decimal To Hexadecimal
- Decimal To Decimal
- Hexadecimal To Decimal
- Hex, Octal, Binary

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

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

I constanty check ur LordofNet blog. Nice work

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

Nice try. Keep up the good work.

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

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

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 ;)

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!

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.

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

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

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.

I have to agree with naters and stipa (partially). You lost me at negative binaries; what is a

complementtill base? The rest of the tutorial was mildly informative, probably not for n00bs.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?

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.

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?

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.

Negative Binary Numbers part is very Confusing.

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

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.

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.

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

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

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

good material! the presentation is good as starting material

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

I want to study programming c

you guid me about it