addition modulo 32

Windows 10, Visual Studio 2019, C++
I am working on some code that will include SHA-256. In my readings about it, articles mention the use of “addition modulo 32.” When I read about that operation the effect seems to be just add the two 32 bit numbers and disregard any carry into bit 33. The words used go into much more mathematical detail and I am concerned that I am missing something. Do I have the essence correct?
If we're talking about unsigned numbers, then yeah that's pretty much it. The bit math works out nicer when the ring modulo n is a power of 2.

The most simplified version of this would of course be addition modulo 2, where 1 + 1 = 0 carry 1, or just 0.
Last edited on
Thanks for the simplification and confirmation.
Is there any specific C++ syntax to do this?
If I declare some unsigned ints of size 32 and just add them it seems that would do this trick automatically.
that is correct. It may be 'technically' undefined to use the result of an overflow, but in practice, pretty sure all platforms drop the overflow and the result is like a modulo.

I found that value to hex string took longer than the shuffle: sha is pretty fast math side.
Last edited on
Unsigned arithmetic does not overflow.
http://www.eel.is/c++draft/basic.fundamental#note-2
Thanks for the link from JLBorges.

The information there needs an update. The use of signed and unsigned char, int, long int, etc, is imprecise and is not standardized. If the size matters, use stdint.h. It has a set of precise definitions:

1
2
3
4
int8_t         uint8_t
int16_t        uint16_t
int32_t        uint32_t
int64_t        uint64_t


Using those makes the intent absolutely clear and does not change for different platforms.

I knew about that, could not think of the name, and needed a dozen searches before something came up with that name.
Last edited on
bkelly13 wrote:
The information there needs an update. The use of signed and unsigned char, int, long int, etc, is imprecise and is not standardized.

This is a funny thing to say because the page that JLBorges linked to shows a copy of the latest working draft of the C++ Standard.
> Using those makes the intent absolutely clear and does not change for different platforms.

Pedantically, those are optional and need not be available on all platforms.

std::int_fast32_t, std::int_least32_t etc. would be available on all implementations.
https://eel.is/c++draft/cstdint.syn
Topic archived. No new replies allowed.