Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

lukar

Addition and subtraction op

This topic is 5277 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi im making my own c++ class for a 64 bit int before I try on doing any harder big number classes. I was able to work on the positive + positive part of the addition operator but now im having trouble with negative + negative. Im managing negative numbers with complements of two. I will post my whole code first and then post the parts which give me trouble
#include <iostream.h>
#include <conio.h>
#define LIMIT 0x80000000
//nota no podemos tener numeros mayores al hexadecimal 0x80000000


class VH {
	private:
   	int dat[4];
      int carry;
   public:
   	void operator=(VH);
      VH operator+(VH);
      VH operator-();
      VH operator-(VH);
      VH sumaPositivos(VH);
      VH restaPositivos(VH);
      bool signo(int *arg);
      void Desplegar();
   	VH(int *);
      VH ();
};


VH::VH()
{
	for(int i = 0; i < 4; i++)
   	dat[i] = 0;
}

VH::VH(int arg[])
{
	for(int i = 0; i < 4; i++)
   	  dat[i] = arg[i];
}
void VH::Desplegar(){
	for(int i = 0; i < 4; i++)
   {
   	cout << hex << " " << dat[i];
   }
   cout << endl;
}
void VH::operator=(VH op)
{
	for(int i = 0; i < 4; i++)
   	dat[i] = op.dat[i];
}
VH VH::sumaPositivos(VH op)
{
	VH *temp = new VH();
   for(int i = 0; i < 4; i++)
   {


   	if(dat[i] + op.dat[i] >= LIMIT)
      {
      	temp->dat[i] = dat[i] + op.dat[i]+carry - LIMIT;
      	carry = 1;
      }
      else
      {
      	temp->dat[i] = dat[i] + op.dat[i]+carry;
      	carry = 0;
       }


   }
   return *temp;
}
VH VH::restaPositivos(VH op)
{
	VH *temp = new VH();
   *temp = *this;
   for(int i = 0; i < 4; i++)
   {
   	if(dat[i]<op.dat[i])
      {
         temp->dat[i+1]= temp->dat[i+1]-1;
         temp->dat[i] = temp->dat[i] + LIMIT - op.dat[i];
      }
      else
      {
      	temp->dat[i]=temp->dat[i] - op.dat[i];
      }
   }
	return *temp;
}
VH VH::operator-(VH op)
{
	if(dat[1] < LIMIT && op.dat[1] < LIMIT)
   {
   	return restaPositivos(op);
   }
}
VH VH::operator+(VH op)
{
	if(dat[1] < LIMIT && op.dat[1] < LIMIT) //I dunno if this is correct checking for a positive or negative number

   {
   	return sumaPositivos(op);
   }
}
VH VH::operator-() //Just used to negate numbers

{
	VH *temp = new VH();
   carry=1;
	for (int i = 0; i < 4; i++) {
		temp->dat[i]=~dat[i]+carry;
		if (~dat[i]==0xFFFFFFFF && carry==1){
			carry=1;
		}
      else {
			carry=0;
		}
	}
	return *temp;
}
void main()
{
	int r[] = {0,0x30,0,0};  
   int s[] = {0,0,0,0x40}; //read from right to left 0x40 0 0 0

   VH d(r);
   VH b(s);
   VH a;
   a = d-b;
   a.Desplegar();
   getch();
}
As you can see i have two big positive numbers there Now this is the paart which gives me trouble...
VH VH::restaPositivos(VH op)
{
	VH *temp = new VH();
   *temp = *this;
   for(int i = 0; i < 4; i++)
   {
   	if(dat[i]<op.dat[i])
      {
         temp->dat[i+1]= temp->dat[i+1]-1;
         temp->dat[i] = temp->dat[i] + LIMIT - op.dat[i];
      }
      else
      {
      	temp->dat[i]=temp->dat[i] - op.dat[i];
      }
   }
	return *temp;
}
I dunno if anyone could help me... i don''t know what im doing wrong. I would also like some tips on how to do: Negative - Negative Negative + Negative and the possible combinations... I beg for your knowledge

Share this post


Link to post
Share on other sites
Advertisement
If you''re using two''s complement representation, then the great thing is that ALL operations are exactly the same with negative numbers as with positive numbers. The only difference is how you print them out.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!