473,657 Members | 2,283 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

subtration of bytes using array of bytes

10 New Member
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
3 3012
newb16
687 Contributor
@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
susheela s
10 New Member
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
donbock
2,426 Recognized Expert Top Contributor
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

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

Similar topics

1
2003
by: Rvenkatesh | last post by:
How to use array string in C#.Net to compare and replace characters between strings using function.
19
2856
by: becte | last post by:
I need to use three bytes to store four 6-bit integers (4 * 6 = 3 * 8) like this 11111122|22223333|33444444 Suppose the input is, int c1, c2, c3, c4, range 0 .. 2^6 -1 and the output is int o1,o2,o3, range 0 .. 2^8-1 How to do this in a clever way? (The 6 bits integers represent characters in range A-Z and 0-9)
1
2986
by: nel | last post by:
hi.. i want to know how to insert data using array in mysql where the data stored in field given? this field_name i taken from database.... thanks a lot...
2
2566
by: palani12kumar | last post by:
Hi i've implemented a linear Queue using array. But i've been struck up with a doubt!!!!! Let me tell my problem with an example so that it can be clear. Size of the Queue(array) is 5. im initializing the front and rear to 0. I continously add five elements to the Queue, so that now the rear points to the 4th position of the array and now the queue is full. Now i delete all the five elements and now the Front and the Rear points at...
1
3677
shoonya
by: shoonya | last post by:
i am using array as a data type in my db .... ( sequence integer ) ; and it stores a value let {1,2,3,4,5}
1
1731
by: sriprasanna | last post by:
Hello All, In our project we have to store PDF, word document files in DataBase(SQL SERVER 2005). We have planned to convert the file to array of bytes and store the binary file into database. We are using c#, ASP.NET 2.0. How to convert a PDF file and a Word Doc file to array of bytes? Is there any other means to implement this.
1
2221
by: George2 | last post by:
Hello everyone, I am using Windows Server 2003 Performance Counter tool to monitor the memory consumed by my process. The interested terms are working set, virtual bytes and private bytes. My questions are, 1. If I want to watch the real physical memory consumed by current process, which one should I monitor? 2. If I want to watch the physical memory + swap file consumed by current process, which one should I monitor? 3. Any more...
2
5209
by: berrylthird | last post by:
This question was inspired by scripting languages such as JavaScript. In JavaScript, I can access members of a class using array syntax as in the following example: var myInstance:myClass = new myClass(); myInstance.member_0 = memberValue_0; // absolute notation myInstance = memberValue_0; // relative notation myInstance = memberValue_0; // nominal notation You can initialize a struct in C/C++ like you would an array, as with the...
0
8411
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8323
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
8838
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7351
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5638
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
4173
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...
1
2740
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
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.