435,294 Members | 2,687 Online + 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 #include #define N 2   typedef unsigned char byte;   class Megaint  { private:       byte a[N];      unsigned int R[N]; public:     Megaint();     Megaint(byte m[]);     Megaint operator-(Megaint M);     void ShowMegaint(); };   int sub(byte n1,byte n2,byte n3) {     int N1,N2,N3,N4;     N1=n1;N2=n2;N3=n3;     if((N1>10)&&(N1<100))         N2=N2*100;     else if(N1>100)         N2=N2*1000;     else          N2=N2*10;       N1=N2+N1;     N4=N1-N3;     return N4; }   Megaint::Megaint() {     int i;     for(i=0;iM.a[i])             res.R[i]=a[i]-M.a[i];         else         {                 R1=sub(a[i],a[i+1],M.a[i]);               res.R[i]=R1;               res.R[i]=res.R[i]&0x0ff;               R1=R1>>8;               a[i+1]=R1;             }     }     return res; }     void Megaint::ShowMegaint() {     int i;     cout<<"the ans is "<>A[i];     }       cout<<"enter 2nd elements"<>B[i];     }*/       Megaint first(A),second(B),third;       third=first-second;     third.ShowMegaint();       return 0; } thank u Sep 10 '09 #1
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 