A
A
A_Htke2020-11-26 20:07:25
C++ / C#
A_Htke, 2020-11-26 20:07:25

How to find half sum of 32 bit signed numbers?

The input file is given eight bytes that define two 32-bit signed integers: the first four bytes contain the number A, and the last four bytes contain the number B. You need to calculate the half-sum of these numbers, rounding the result down. Output the resulting half-sum to the output file as a 32-bit signed integer. All three numbers are given in little-endian byte order. Note: A and B can be any number in the range of signed 32-bit integers (i.e. INT_MIN to INT_MAX). It is strongly recommended to test the solution on numbers close to the extreme values, as well as on different combinations of parity numbers. It may be more convenient to use 64-bit integers for intermediate results to avoid overflow. All input and output in this problem is binary. The hex representation of the binary data is shown below: each group of two digits represents one byte in the file. Your program will be given a file with 8 bytes of data as input, and the program should create a file with 4 bytes of response. To create, edit, and view binary files, use a Hex editor such as HxD.
I encounter such a difficult task for the first time, as I am just starting to learn C. Please help me, I'm at a complete loss :(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2020-11-27
@A_Htke

Can't read binary? There is a read function .
Read it 4 bytes 2 times in char buf[4].
little-endian means the smallest bytes go first. Those. buf[0] is the low 8 bits of the number, buf[3] is the high bits. To put a number together, look at the shift operation. (int)buf[3] << 24 | (int)buf[2] << 16 will put in place the 2 high bytes (think about the low ones yourself).
The type of 64-bit numbers is long long. You were advised to use it.
Can you add 2 numbers?
Binary output is done, all of a sudden by the write function .
Well, the input and output files may also need to be opened via the fopen function .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question