By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,294 Members | 2,687 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,294 IT Pros & Developers. It's quick & easy.

subtration of bytes using array of bytes

P: 10
Hi.
Hers im subtracting two 16bit numbers by using two 8bit num array.In case if first byte of array is less than first byte of second array den i hav to take borrow form second byte of first array.This i could do ..wat my doubt s suppose if i take 4 elements in array and first byte of first array is less than first byte of second array den im gonna take borrow from second byte of first array,if that second byte of first array is 0 den how can i take borrow from third byte of first array..plz suggest me some ints

This is the pgm which i did for 2bytes is array
Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. #define N 2
  3.  
  4. typedef unsigned char byte;
  5.  
  6. class Megaint 
  7. {
  8. private:
  9.  
  10.     byte a[N];
  11.      unsigned int R[N];
  12. public:
  13.     Megaint();
  14.     Megaint(byte m[]);
  15.     Megaint operator-(Megaint M);
  16.     void ShowMegaint();
  17. };
  18.  
  19. int sub(byte n1,byte n2,byte n3)
  20. {
  21.     int N1,N2,N3,N4;
  22.     N1=n1;N2=n2;N3=n3;
  23.     if((N1>10)&&(N1<100))
  24.         N2=N2*100;
  25.     else if(N1>100)
  26.         N2=N2*1000;
  27.     else 
  28.         N2=N2*10;
  29.  
  30.     N1=N2+N1;
  31.     N4=N1-N3;
  32.     return N4;
  33. }
  34.  
  35. Megaint::Megaint()
  36. {
  37.     int i;
  38.     for(i=0;i<N;i++)
  39.     {
  40.         a[i]=0;
  41.         R[i]=0;
  42.     }
  43. }
  44.  
  45.  
  46. Megaint::Megaint(byte m[])
  47. {
  48.     int i;
  49.     for(i=0;i<N;i++)
  50.     {
  51.         a[i]=m[i];
  52.  
  53.     }
  54. }
  55.  
  56.  
  57. Megaint Megaint::operator -(Megaint M)
  58. {
  59.     Megaint res;
  60.     int i,R1,count=1;
  61.     for(i=0;i<N;i++)
  62.     {
  63.         if(a[i]>M.a[i])
  64.             res.R[i]=a[i]-M.a[i];
  65.         else
  66.         {
  67.  
  68.               R1=sub(a[i],a[i+1],M.a[i]);
  69.               res.R[i]=R1;
  70.               res.R[i]=res.R[i]&0x0ff;
  71.               R1=R1>>8;
  72.               a[i+1]=R1;
  73.  
  74.  
  75.         }
  76.     }
  77.     return res;
  78. }
  79.  
  80.  
  81. void Megaint::ShowMegaint()
  82. {
  83.     int i;
  84.     cout<<"the ans is "<<endl;
  85.     for(i=0;i<N;i++)
  86.     {
  87.         cout<<R[i]<<endl;
  88.     }
  89. }
  90.  
  91.  
  92. int main()
  93. {
  94.     int i;
  95.     byte A[N]={0x91,0x05},B[N]={0x97,0x02};
  96.     /*cout<<"enter 1st array elements"<<endl;
  97.     for(i=0;i<N;i++)
  98.     {
  99.         cin>>A[i];
  100.     }
  101.  
  102.     cout<<"enter 2nd elements"<<endl;
  103.     for(i=0;i<N;i++)
  104.     {
  105.         cin>>B[i];
  106.     }*/
  107.  
  108.     Megaint first(A),second(B),third;
  109.  
  110.     third=first-second;
  111.     third.ShowMegaint();
  112.  
  113.     return 0;
  114. }
thank u
Sep 10 '09 #1
Share this Question
Share on Google+
3 Replies


100+
P: 687
@susheela s
My dictionary translates 'den' to something absolutely not fitting this context like 'lair' and 'hideout'. Can you please clarify?

Also I don't understand the purpose of 'R' array in megaint - what data is it supposed to hold? Result of operation must be placed into 'a' anyway. Then, operator- takes and returns references to an object.
Besides that, you need function to take two bytes of operand, borrow from previous operation, and return result and borrow from this operation, like
subtr(unsigned char a, unsigned char b, unsigned char (or bool...) borrow, unsigned char& result, unsigned char& rborrow);
Then rborrow = b+borrow > a; // will they be cast to int before addition? afaik yes.
and result = a-b-borrow; // is it safe to assume that (unsigned char)(-1) == 0xff ? if not, proceed underflow separately...

Then call it for all N elements of the array.
Sep 10 '09 #2

P: 10
Now i will be clear suppose if i have two bytes in array(05,06) and (07,03)
06 05
03 07
............
02 fe
in this case i took a borrow from second elelment of array1 to do first byte subtraction since 05 is less than 07..
If i want to extent this to any no bytes..how do i do my doubt is

09 00 05
05 02 08
.............
I this case 05 is less than 08 so e need to take a borrow from second byte but second byte in array is 00..therefore now i need to take borrow from third element 09..how do i do this..plz give any int.
Sep 15 '09 #3

Expert 100+
P: 2,400
I suggest you start by considering how you would implement a similar function that worked with an array of BCD values. A BCD (binary-coded decimal) value is a variable whose value is only allowed to range from 0 to 9. This should be a familiar problem -- its how you learned subtraction in elementary school. Once you're comfortable expressing that familiar rule as a software algorithm, it should be easy to generalize to an array of char's.
Sep 15 '09 #4

Post your reply

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