Showing posts with label Mathematical Operations. Show all posts
Showing posts with label Mathematical Operations. Show all posts

Add two unsigned integers without using '+'

I found this very interesting discussion (and the program) to add two numbers without the use of the arithmetic operator '+'. The obvious guess would be to use the Bitwise operators.

The logic behind the addition operation using the bitwise operators is as follows:


integer1 =  3  = 0011b
integer2 = 5 = 0101b

first operation second operation third operation
0011 0011
shift by one
0101 0101

______ ______
XOR 0110 AND 0001 ------> <<1 0010

Again...
first operation second operation third operation
previous XOR 0110 0110 shift by one
previous <<1 0010 0010
______ ______
XOR 0100 AND 0010 ------> <<1 0100

Again...
first operation second operation third operation
previous XOR 0100 0100 shift by one
previous <<1 0100 0100
______ ______
XOR 0000 AND 0100 ------> <<1 1000

Again...
first operation second operation
previous XOR 0000 0000
previous <<1 1000 1000
______ ______
XOR 1000 AND 0000
When AND iguals 0 the result of XOR is iqual to the sum of the two integers

The program is as follows:



//Program to add two numbers by using boolean operators
//Ref: http://www.daniweb.com/forums/thread84950.html
//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
#include<iostream>

using namespace
std;

unsigned long
add(unsigned integer1, unsigned integer2)
{

unsigned long
xor, and, temp;

and
= integer1 & integer2; /* Obtain the carry bits */
xor
= integer1 ^ integer2; /* resulting bits */

while
(and != 0 ) /* stop when carry bits are gone */
{

and
<<= 1; /* shifting the carry bits one space */
temp = xor ^ and; /* hold the new xor result bits*/
and
&= xor; /* clear the previous carry bits and assign the new carry bits */
xor
= temp; /* resulting bits */
}

return
xor; /* final result */
}


int
main()
{

int
num1 = 13, num2 = 27;

cout << num1 << " + " << num2 << " = " <<add(num1, num2) << endl;

return
0;
}



The output is:
13 + 27 = 40

See the original discussion here.

Number of bits to represent an arbitrary positive number X

Simple program to find the number of bits required to represent a positive number.



//This program is to find out the number of bits required to represent a positive integer
//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy

#include<iostream>
#include <cmath>

using namespace
std;

unsigned int
TraditionalApproach(const unsigned int& num)
{

if
(num)
{

return
(int)floor(log((double)num)/log(2.0) + 1.0);
}

return
1;
}


unsigned int
SimplifiedApproach(const unsigned int& num)
{

if
(num)
{

unsigned int
tempNum = num;
unsigned int
numOfBits = 0;
while
(tempNum)
{

numOfBits++;
tempNum >>= 1;
}

return
numOfBits;
}

return
1;
}



int
main()
{

//Finding number of bits the traditional way
cout<<"\n** Traditional Approach **\n";
cout<<"The number of bits required to represent 0 = "<<TraditionalApproach(0)<<endl;
cout<<"The number of bits required to represent 1 = "<<TraditionalApproach(1)<<endl;
cout<<"The number of bits required to represent 15 = "<<TraditionalApproach(15)<<endl;
cout<<"The number of bits required to represent 16 = "<<TraditionalApproach(16)<<endl;
cout<<"The number of bits required to represent 75 = "<<TraditionalApproach(75)<<endl;
cout<<"The number of bits required to represent 125 = "<<TraditionalApproach(125)<<endl;
cout<<"The number of bits required to represent 130 = "<<TraditionalApproach(130)<<endl;

cout<<"\n** Simplified Approach **\n";
cout<<"The number of bits required to represent 0 = "<<SimplifiedApproach(0)<<endl;
cout<<"The number of bits required to represent 1 = "<<SimplifiedApproach(1)<<endl;
cout<<"The number of bits required to represent 15 = "<<SimplifiedApproach(15)<<endl;
cout<<"The number of bits required to represent 16 = "<<SimplifiedApproach(16)<<endl;
cout<<"The number of bits required to represent 75 = "<<SimplifiedApproach(75)<<endl;
cout<<"The number of bits required to represent 125 = "<<SimplifiedApproach(125)<<endl;
cout<<"The number of bits required to represent 130 = "<<SimplifiedApproach(130)<<endl;

return
0;
}



The output is as follows:

Fixed form, Scientific form and Precision setting for floating point numbers

Sometimes we want to output the floating point numbers in fixed or scientific form and sometimes with fixed precision so as to align all the output. The following program is an example to demonstrate that:



//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//This example shows how to output floating point numbersin fixed and
#include<iostream>

using namespace
std;

int
main()
{

float
number = 123.456;

cout << "number in fixed form = " << number << endl; //default is fixed
cout << "number in scientific form = " << scientific << number << endl;
cout.precision(2);
cout << "number in fixed form with precision 2 = " << fixed << number << endl; //here the format was scientific
cout.precision(3);
cout << "number in fixed form with precision 3 = " << number << endl;
cout.precision(4);
cout << "number in fixed form with precision 4 = " << number << endl;
cout.precision(5);
cout << "number in fixed form with precision 5 = " << number << endl;

return
0;
}






The output is as follows:

Printing out leading 0's (or any other charachters)

This is a simple example to print out leading zeroes or any charachters when printing out numbers. The main manipulator function needed is the set field width that will set the width of the field.



//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//This example shows how to print leading 0's in the output

#include<iostream>
#include<iomanip>

using namespace
std;

int
main()
{

int
number = 123;

cout<< "number = "<< number << endl;

cout<< "number = "<< setw(2) << setfill('0') << number << endl;
cout<< "number = "<< setw(3) << setfill('0') << number << endl;
cout<< "number = "<< setw(4) << setfill('0') << number << endl;
cout<< "number = "<< setw(5) << setfill('0') << number << endl;
cout<< "number = "<< setw(10) << setfill('0') << number << endl;

cout<< endl;
cout<< "number = "<< setw(5) << setfill('x') << number << endl;

cout<< endl;
number = 0;
cout<< "number = "<< setw(5) << setfill('x') << number << endl;

return
0;
}







The output is as follows:

Check out this stream