How to perform binary add by type of mod in Ada?

A very specific problem here ... and no, this is not homework (far away ... far behind). Basically I need to calculate the checksum for writing EPROM and Id code in order to write this function in the Ada program in order to practice manipulating my bits in the language.

The section of the firmware data file for EPROM changes me, and this change requires a new valid checksum at the end, so that the resulting system accepts the changed code. This checksum starts with modulo 256 executing a binary sum of all the data it covers, and then other higher-level operations are performed to get a checksum that I will not enter.

So, how do I make a binary addition by type of mod?

I suggested that if I use the "+" operator for a mod type, it will be summed up as an integer value operation ... I don’t want the result. Im really stalled on this. I don’t really want to make a packed array and perform bit wrapping if I don’t need it, especially if it is considered an “old hat”. Im reading links claim that you need to use mod types to provide more portable code when working with binary operations. I'd like to try if possible. I am trying to target multiple platforms using this program, so portability is what I am looking for.

Can anyone suggest how I can do binary add by type of mod?

Any starting places in this language will be very useful.

+4
source share
1 answer

Just use the modular type , for which operators perform unconditional arithmetic.

type Word is mod 2 ** 16; for Word'Size use 16; 

Application: for modular types, predefined logical operators work in stages. Moreover, "the binary operators of adding + and - on modular types include the final reduction modulo if the result is outside the basic range of the type." function Update_Crc is an example.

Appendix: §3.5.4 Integer types, ¶19 notes that for modular types, the results of predefined operators are reduced modulo, including the binary addition operators + and - . In addition, the shift functions in §B.2 Package interfaces are available for modular types. Taken together, arithmetic, logic, and shift capabilities are sufficient for most bitwise operations.

+5
source

All Articles