473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

All Arithmatic operations using Bitwise operators only

Is it possible to perform all arithmetic operations (+,-,*,/) using Bitwise operations only. Please consider signed numbers also.
Thank You.
Dec 25 '08 #1
14 19091
weaknessforcats
9,208 Expert Mod 8TB
Of course. That's how the processor does it.
Dec 25 '08 #2
@weaknessforcats

Yes, I have a method for addition and subtraction given below

Expand|Select|Wrap|Line Numbers
  1. int add(int a, int b)
  2.            do
  3.            {
  4.                       a=a^b;
  5.                       b=(a^b)&b;
  6.                       b=b<<1;
  7.            } while(b);
  8.  
  9.            return(a);
  10. }
  11.  
Basically using this method, one can perform Multiplication and Division operations.
Now, is there any other specialized methods for Multiplication and Division operations? e.g. (num<<1) is like (num*2) and (num>>2) is same as (num/2).
Here we are not using the addition or subtraction operations, but directly obtaining results of Multiplication and Division operations. Please explain any such method.
Please give example of processors methods of these operations(+,-,*,/).
Thank You.
Dec 25 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
I don't see a question in your reply. In any case, I cannot provide solutions, act as a tutor or do research for you.

Please read the posting guidelines.
Dec 26 '08 #4
Bassem
344 100+
Hi Aftabpasha,

It is true that you can perform all arithmetic operations using the two basics operations + and -.
I have studied Z80 Microprocessor by mad by Zilog, it offers only ADD, SUB, INC (increment), and DEC (decrement) arithmetical operations (beside Logic operations), we typed programs in assembly to make Multiplication and Division, and what amazing is that it still produced and successes until now in many new and complicated applications.


the next Microprocessor generation 8086 provides MUL and DIV operations, and an improvement in H/W (Hardware) like increasing the addressing capacity, data bus 16 bit and pipelining etc.


But nowadays CPUs with high Memory capacity and provide very complex operations like Log, antiLog, tan, cos, .... and that is what you use in home and me and everyone else.
Dec 27 '08 #5
Bassem
344 100+
Here is Example


Multiplication of two numbers
int MULT(int x, int y)
{
int n = x;
while (n) {
x += y;
n--;
}
return y;
}

Division of two numbers
int DIV(int x, int y)
{
int n = 0;
while (x > y) {
x -= y;
n++;
}
return n; // truncate fraction
}
Dec 27 '08 #6
donbock
2,426 Expert 2GB
Is this assignment for a programming class or a Boolean algebra course? You should start with addition: google the term "half adder". You can add two binary numbers by stringing together enough half adders. Once you have addition working it is simple to get subtraction: google the term "two's complement". Multiplication is repeated addition; division is repeated subtraction.
Dec 27 '08 #7
Bassem
344 100+
I worked on a personal Microprocessor Simulator last year, where i used basic logic gates (AND, OR, XOR) to perform all mathematical operations provided by the MP using Boolean to represent data in registers.

Introduce a big piece of code may confuse or tiered, so i expected these methods to declare the way of thinking, not to explain the whole idea.

Regards
Dec 27 '08 #8
JosAH
11,448 Expert 8TB
@Bassem
Don't do it that way; it takes ages to complete; the Russian Peasant Method is more clever:

Expand|Select|Wrap|Line Numbers
  1. int mul(int x, int y) {
  2.    int p;
  3.    for (p= 0; x; x>>= 1, y<<= 1)
  4.       if (x&1) p+= y;
  5.    return p;
  6. }
  7.  
kind regards,

Jos
Dec 28 '08 #9
Banfa
9,065 Expert Mod 8TB
Bassem, your multiplication algorithm is wrong you return y which is unchanged by the function.

Everyone, you are left shifting a signed value. For C (and I assume C++ but I don't actually know) this is a platform defined operation, that is different platforms may implement it differently producing different results (normally depending of whether the platform performs an arithmetic or logical shift).

For portability it is only safe to shift unsigned values.
Dec 28 '08 #10
JosAH
11,448 Expert 8TB
@Banfa
You still must have eggnog in your brains ;-) A left shift is a logical shift to the left; a right shift shifts in the sign bit (by definition) if an artithmetic shift is used with a signed int, otherwise a zero bit is shifted in (a logical shift). But you're right: an unsigned shift has always been more 'natural' to me as well ;-)

kind regards,

Jos
Dec 28 '08 #11
Thank you all for your replies. So, from these posts, I think, one can say that there can be different methods for performing the basic arithmetic operations using bit-wise operators on different machines, and therefore it is necessary to know how certain basic bit-wise operations are performed on different platforms before using them in coding. Otherwise it is better to use normal arithmetic operators (+,-,*,/), specially when you want platform-independent code.
Thank you all once again.
Kind Regards.
Dec 30 '08 #12
donbock
2,426 Expert 2GB
It is _always_ better to use normal arithmetic operators to perform arithmetic functions. The exceptions to this simple rule are so exceptional that you're unlikely to ever run into them.
@Aftabpasha
Dec 30 '08 #13
Bassem
344 100+
Thanks a lot Banfa,
I ment to return x not y.

As you said the platform who provides the operations, we just use.

Josah, your idea is clear using Half-Adder or Full-Adder, and that leads me to ask why Bytes.com have no community for Microprocessors / Microcontrollers ?

It may be replaced to suggestions (Dept.) - if it's correct to say- Thanks anyway,
I get widely help from your (all of you) answers for many questions other put.

Regards for all,
Bassem
Dec 30 '08 #14
Thank you all. Donbock, I'll remember your advice.

Regards,
Aftab
Dec 31 '08 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Jason | last post by:
I am going through the Geek Challenges on the Open Source Institute Website: http://www.osix.net/modules/geek/ The instructions for Level 4 are: "This challenge requires you to use some of...
4
by: Mike Hodkin | last post by:
As a beginning student of C++, books reference "bitwise operators" and give brief examples, but I have not read a good explanation of what they are used for. One reference mentioned that they are...
14
by: Tony Johansson | last post by:
Hello Experts! Assume I have a class called SphereClass as the base class and a class called BallClass that is derived from the SphereClass. The copy constructor initialize the left hand object...
9
by: Christopher Weaver | last post by:
I know that the bitwise AND of 8 and 4 will return 0 or false and the bitwise AND of 8 and 9 will return 1 or true but I don't know how to write the synax for it in C#. I have a value that ranges...
10
by: Emilio | last post by:
Do I use 'or' for bitwise operations where in c# I use | ?
4
by: AMDRIT | last post by:
Gang, I always get confused when it comes to 1's and 0's. I would like to perform a bitwise operation on a value based on checked boxes. Am I doing this right? assuming...
5
by: noridotjabi | last post by:
I'm learning to program in C and any tutorial or book that I read likes to briefly touch on birdies operators and then move on without giving any sort of example application of them. Call me what...
16
by: Shawnk | last post by:
I would like to perform various boolean operations on bitmapped (FlagsAttribute) enum types for a state machine design as in; ------------------- enum portState { Unknown, Open,
8
by: Daniel Gutson | last post by:
Hi, I just wanted to share another library for doing type-safe bitwise operations in C++: http://bitwise-enum.googlecode.com I found it useful, so hopefully it'll be for somebody else as well....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.