mlyszczek commented on PR #6613:
URL: https://github.com/apache/incubator-nuttx/pull/6613#issuecomment-1186594715

   > > Yea, there won't be a "-1" bug now, but this will trigger an assert for 
unsigned ll bigger than `ULONG_MAX/2`. I guess you could do:
   > > `DEBUGASSERT((LONG_MIN <= x && x <= LONG_MAX) || (0 < x && x <= 
ULONG_MAX));`
   > > To handle both signed and unsigned cases.
   > 
   > Now I don't understand it. Can you explain how does this assert work? Do 
integer comparisons depend on signedness in C? Then it should be 0UL..? What is 
the type of x and does it matter (type promotion)?
   
   You can't compare signed and unsigned. If you try to compare signed and 
unsigned, signed value will be converted to unsigned. And so, considering 8bits 
vars, ```-1 < UCHAR_MAX``` will return ```false```, when you would expect it to 
return ```true```. Reason for this is, that netagive numbers are represented as 
2 complement, so -1 in bit representation will be ```11111111``` (-2 will be 
```11111110``` and so on) which is equal to ```UCHAR_MAX```.
   
   ```(LONG_MIN <= x && x <= LONG_MAX)```
   This part only checks if ```x``` is within signed values, so around 
+-2million. Unsigned values can be bigger and can be up to around 4million. If 
you'd pass 4million value, assert would trigger as 4million is bigger than 
2million. That's where the second part comes in.
   
   ```(0 < x && x <= ULONG_MAX)```
   It simply checks, if number is bigger than 0, then check if it's lower than 
4million. So for 4million value, first part will return ```false``` but second 
will return ```true``` so whole assert will be true and it won't trigger.
   
   0 in that case will be promoted to unsigned long.
   
   > I naively think atm that this assert checks whether 64-bit x is a valid 
(sign-extended representation of a) 32-bit integer from LONG_MIN to ULONG_MAX 
inclusively.
   
   
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to