1. ## Challenge for newbs

First some context, I've been making a bignum type project, encountered an error with my code's results, don't have time to fix it myself as I have to go to work, if no-one posts a fix by the time I return to this then I'll just fix it myself. As you may have guessed by now the challenge is to fix just one function, this one:
Code:
```int _alu_add( alu_t *alu, int num, int val )
{
bool carry = 0;
int ret = _alu_check2( alu, num, val );
alu_reg_t *N, *V;
alu_bit_t n, v = {0}, e;

if ( ret != 0 )
return ret;

N = alu->regv + num;
V = alu->regv + val;

n = N->init;
v = V->init;
e = N->upto;

for ( ; n.b < e.b; alu_bit_inc( &n ), alu_bit_inc( &v ) )
{
if ( carry )
{
if ( !(*(n.S) & n.B) )
{
*(n.S) |= n.B;
carry = false;
}
}

if ( *(v.S) & v.B )
{
if ( *(n.S) & n.B )
{
carry = true;
*(n.S) ^= n.B;
}
else
*(n.S) |= n.B;
}
}

return carry ? EOVERFLOW : 0;
}```
I've attached the full project in case you feel like taking on the far harder challenge of finishing it, I'll still finish it myself but doing so can give you the experience of jumping into other developer's code and complete unfinished code which could prove useful in future jobs.

You can also use the project to compile and see why I know the function is faulty

2. For anyone who actually tried to figure it out it turned out the problem lied in alu_get_reg() and alu_setup_reg(), I had forgotten to update the alu->*.qty.used values so I ended up adding values to themselves

3. Originally Posted by awsdert
For anyone who actually tried to figure it out
Nobody tried to "figure it out".

4. Originally Posted by john.c
Nobody tried to "figure it out".
How would you know that? Are you God? Of course not, don't go assuming things instead of verifying, just because I happen to believe you're right does not mean I know you're right, very big difference there, hence for the case I was wrong I left a statement, besides which some poor sod could find this thread months/years later and try and fix a non-broken function instead of looking at the right function to fix, after all I did say it's a challenge for newbs which means they might actually take up the challenge to test their own understanding of C

5. Originally Posted by awsdert
For anyone who actually tried to figure it out it turned out the problem lied in alu_get_reg() and alu_setup_reg(), I had forgotten to update the alu->*.qty.used values so I ended up adding values to themselves
As it turns out there was a minor mistake in the addition function after all, I forgot to clear the bit in the case there was a bit when carry is being applied, if you understood binary addition well enough then it won't take you long to understand what I mean here without me posting the code. (Again I post this not expecting there have been anyone who downloaded the project to challenge themselves but in case I'm wrong I'll point you in the right direction as this one is subtle)