473,893 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

All Arithmatic operations using Bitwise operators only

32 New Member
Is it possible to perform all arithmetic operations (+,-,*,/) using Bitwise operations only. Please consider signed numbers also.
Thank You.
Dec 25 '08 #1
14 19142
weaknessforcats
9,208 Recognized Expert Moderator Expert
Of course. That's how the processor does it.
Dec 25 '08 #2
Aftabpasha
32 New Member
@weaknessforcat s

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 Recognized Expert Moderator Expert
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 Contributor
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 Contributor
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 Recognized Expert Top Contributor
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 Contributor
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 Recognized Expert MVP
@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 Recognized Expert Moderator Expert
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

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

Similar topics

4
3108
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 your favourite mathematical operators, and also get to do simple simple file IO. Additionally you will learn bit operations. # download this file. Process every byte, if it is greater than 30
4
2493
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 used in hardware programming. Are they used very often in routine C/C++ programming, and what for? thanks, MJH
14
2369
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 in this case object b2 below. It also initialize subobject from class SphereClass. The BallClass copy constructor looks like this. BallClass::BallClass(const BallClass& bc) : SphereClass(bc)
9
7033
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 from 0 to 15 and I need to compare it to 15 in order to find if it contains the values 1, 2, 4, or 8. To represent it more graphically, the value 1010 when ANDed with 1000 or 0010 will produce either true or a value greater than 0. I believe...
10
2187
by: Emilio | last post by:
Do I use 'or' for bitwise operations where in c# I use | ?
4
1887
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 TurmsRemote.Weekdays has values of
5
5805
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 you will but I cannot seem to see the purpose for bitwise operators. Especially the operators bitwise OR ( | ) and bitwise AND ( & ), I'm just not getting it. I have searched around and really haven't found anything that gave explanation to why...
16
3261
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
8796
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. BRgds, Daniel.
0
9832
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10839
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10469
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8022
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7173
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5858
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6066
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4684
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4280
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.