-1

I have got the bytes of 2 integers (say 32 bit int) now is it possible to add them using the bytes?

I have like

char b1[4], b2[4];
int a= 2311;
int b= 233134;
memcpy(b1, &a, 4);
memcpy(b2, &b, 4);

My question is is there any algorithm to add, mul, sub the numbers from bytes and the number of bytes of number is not fixed it may be 32 bit, 64 bit, 128 bit.

Note i do not want any library or framewprk just c++

Jaysmito Mukherjee
  • 1,467
  • 2
  • 10
  • 29
  • I'm also wondering about the reason behind the question you ask. Is it just curiosity? That's fine, but please state so in the question itself. Otherwise, what is the actual underlying problem you need to solve? And why do you think the solution you ask about would be good? And if there's an underlying problem, then your question (in its current form) is an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Some programmer dude Dec 09 '21 at 06:59
  • 1
    @Someprogrammerdude not much of a under laying problem. Just learning low level programming and am curious about how hese thing are done. – Jaysmito Mukherjee Dec 09 '21 at 07:01
  • In general, the answer is "No." The problem is endianness. The carry necessarily propagates from least-significant to most-significant. So unless you know the endianness, the carries can't be handled correctly. – user3386109 Dec 09 '21 at 07:02
  • @user3386109 you cna say i know the endianness. Little endian – Jaysmito Mukherjee Dec 09 '21 at 07:03
  • 1
    Then all you need to do is grade-school long addition. Likewise for the other three operations. – user3386109 Dec 09 '21 at 07:04
  • @Someprogrammerdude yeah algorithm for arithmetic on integers of any size 4, 8, 2 – Jaysmito Mukherjee Dec 09 '21 at 07:04
  • @user3386109 will that be fast? – Jaysmito Mukherjee Dec 09 '21 at 07:07
  • 2
    The "simple" solution: Convert back to plain `int` values... ;) – Some programmer dude Dec 09 '21 at 07:10
  • As for long addition, since the values are split not by decimal boundaries but by *binary* boundaries, it should be possible to do it using binary arithmetic instead of decimal. – Some programmer dude Dec 09 '21 at 07:12
  • @Someprogrammerdude cannot convert to int or long as then it would be handled by the compiler as i told i want to have a algorithm that can handle that – Jaysmito Mukherjee Dec 09 '21 at 07:14
  • @Someprogrammerdude i am talking of binary arithmetic but i am very new it it so asking for help – Jaysmito Mukherjee Dec 09 '21 at 07:15

1 Answers1

1

Your question is in its heart not about the implementation in C++, but about algorithms to do simple arithmetic.

For all the operation you mention, remember how you did it in primary school. Apply that algorithms, replacing single decimal digits by bytes. The principle stays the same. It's all mathematics.

You need to think how you detect carries and borrows between the bytes.

Because you mentioned int as data type, you need to take the sign into account. It is easier if the values are unsigned.

the busybee
  • 10,755
  • 3
  • 13
  • 30
  • Could you give a example of how to go about it? Specially for mul and div with bytes – Jaysmito Mukherjee Dec 09 '21 at 07:39
  • And also some way to manage the overflow – Jaysmito Mukherjee Dec 09 '21 at 07:40
  • No, I don't have the hours left for this. Especially multiplication and division need working addition and subtraction. SO is not a code writing service. You can start by implementing addition of unsigned values. Remember primary school: you did not learn to divide before you learned addition and subtraction. ;-) -- Honestly, why would someone want to use bytes? If you need integer arithmetic of big numbers, break it down to the widest type the processor can handle, and use assembler to do the work. It is far more easier to implement it in assembler. – the busybee Dec 09 '21 at 07:50
  • I am working a vm whoch works with bytes – Jaysmito Mukherjee Dec 09 '21 at 10:55
  • Well, then... Go with bytes. The last time I had to implement such functions was 40 years ago with an 8 bit processor. The target data type was 16 bit signed integer. Addition and subtraction was simple, and for multiplication and division I used a binary algorithm. All was implemented in assembler, because a higher level language hides certain capabilities like the carry flag, which make life so much easier. – the busybee Dec 09 '21 at 12:37
  • If your target is some kind of non-standard processor, how could you use C++? Do you have a cross compiler? Anyway, you should implement the stuff in assembler. If you want that, please edit your question accordingly. – the busybee Dec 09 '21 at 12:37
  • Nope not any any non standard processor. And I would too implement this in the compiler for the vm's bytecode. – Jaysmito Mukherjee Dec 09 '21 at 13:22