SIGN UP MEMBER LOGIN:    
ARTICLE

CODE- TALES: Series I

Posted by Suchit Khanna Articles | Algorithms in C# January 23, 2012
CODE-TALES is an effort for passionate technologists who are always out there trying to learn every day. I can assure you that each series will bring a new topic and though there can be several ways to do the same thing so any approach other than penned down here is always welcomed.
Reader Level:
Download Files:
 

In this first series of CODE-TALES, let me take this opportunity to congratulate my friend Dhananjay Kumar on being awarded Telerik MVP (another feather in your cap). He has been a big inspiration for many; we both started our journey from the same place and I still remember when you joined us in MS COE team as a young professional who chose his path to success indifferent to what others thought or said about him. Keep moving forward...

About


CODE-TALES is an effort for passionate technologists who are always out there trying to learn every day, I can assure you that each series will bring a new topic and though there can be several ways to do the same thing, so any approach other than penned down here is always welcomed.

Simple Mathematics Operator

Yes you guessed it right, today we will discuss and see how simple mathematics can be implemented in the world of computers; this question always intrigued me.

Problem

Given the two numbers a and b, I want to add the two numbers (a + b) to get its sum.

Solution

Now we are actually trying to implement the '+' operator, giving our own meaning to the operator. Usually there would be somewhere some compiler of a particular language or Operating System API that would givie us the definition of the + operator and that means somewhere someone would have given (written) that definition, so the question still remains how ?? Well that is what we will discuss in this series.

We will be using the bit operators and logical operators in Digital Electronics to implement our addition operation. So a brief understanding of the same is required (discussion of which is out of scope of this series).

Let us see the pseudo code for the same first:

Repeat:

add = a XOR b
carry = ( a AND b ) << 1
a = add
b = carry
while carry is not zero
return add.

Now let us see how this algorithm works with a help of an example:

Let a = 2, b = 3

Loop I

In the binary world a variable a can be represented as: 0010 and b as: 0011

0010 0010
0011 0011
------- ------
0001 (add) 0010 << 1 = 0100 (carry)

Loop II

Since the carry is not 0, we continue:

0001 0001
0100 0100
------ ------
0101 (add) 0000 << 1 = 0000 (Finish point)

Since here the carry becomes zero so our algorithm stops here and we see the final value of variable add is: 0101 which is decimal equivalent of 5.

Problem

Given two numbers a and b, I want to subtract two numbers (a - b) to get its difference.

Solution

Well we can look at this problem as: a + (-b) or in other words a + (~b + 1)

And we can thus represent subtraction in terms of an addition method which we defined above a short while ago.

Well, this is all we have for this series, in the coming series we will see how we can do the same for multiplication as well as division operation. The code sample for the same is available in C# / C++ / Java language.

 

Login to add your contents and source code to this article
share this article :
post comment
 

Thanks Monica..

Posted by Suchit Khanna Jan 24, 2012

Thanks for appreaciation hope it brings some value add...

Posted by Suchit Khanna Jan 24, 2012

Hi Suchit. Its a very useful and good article.

Posted by Monika Arora Jan 23, 2012

good effort i must say.......

Posted by Sonakshi Singh Jan 23, 2012
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Become a Sponsor