Jump to content

Clever trick or just just useless?


flintstone

Recommended Posts

Hi,

I found the code snippet I attached in a Flexmotion VI and it checks for counter overflow by subtracting the two unsigned long timestamp values and testing whether the result is <0. Now I've tried a similar construct at my machine and it behaves just as expected: The 32 bit counter rolls over as it should but since we are testing an unsigned value to be <0 the test never gives result "true".

Since the code in the "false" branch simply uses the result of the subtraction that should not be a practicat problem but I am wondering whether there is some clever trick behind that or if that is just the useless.

Cheers,

flintstone

By the way: Sorry for the double "just" and for the (succeeded) experiment whether one can rate the own topic.

post-27070-0-87701700-1348560568.png

Edited by flintstone
Link to comment

The timers output unsigned U32 numbers, and the subtraction will produce the same U32. This cannot be less than zero so that code never executes. Which is not a problem since the subtraction will already handle the rollover satisfactorily; for example: 1 − 4294967295 = 2.

Understanding unsigned numbers and rollover is tricky; I usually have to experiment to remind myself how it works every time I have to do it.

— James

Link to comment

Like both of you already surmised, a U32 can't be negative, so the case isn't actually executed. I'm going with "useless".

If I had to guess, I would guess that either the person who did this used an algorithm which was designed for a signed integer or they simply didn't notice that a negative U32 is meaningless. I would say the latter is more likely, because comparing the two numbers would be simpler than subtracting and checking for less than zero. If this was a more standardized algorithm, I'm guessing it would do the comparison.

Link to comment

The timers output unsigned U32 numbers, and the subtraction will produce the same U32. This cannot be less than zero so that code never executes. Which is not a problem since the subtraction will already handle the rollover satisfactorily; for example: 1 − 4294967295 = 2.

Understanding unsigned numbers and rollover is tricky; I usually have to experiment to remind myself how it works every time I have to do it.

— James

I'm not sure where the claim comes from, about that understanding unsigned numbers is difficult. It supposedly is the reason that Java doesn't have unsigned integers since the original Java inventers claimed, that nobody understands them correctly anyhow so a language better does not provide support for them. I still have the feeling that they did not understand it and assumed that what is true for them must be true for everyone. Now if you need to implement binary protocol parsing that contains unsigned integers you really have to go nilly willy to get the right result in Java.

Link to comment

Thanks for your replies. Unsigneds really are not too complicated (could be that my hw developing experience from the past years helps me there a lot).

Yes hardware exposure on digital logic certainly helps the understanding. :D If you think of integers as a simple register or counter, the oddball in the mix is rather the signed integer than the unsigned due to its use of one's complements!

Think about it. The MSB is the sign!

-128 => 0x80

-1 => 0xFF

1 => 0x01

127 => 0x7F

A naive approach to this could be instead (offset notation):

-128 => 0x00

127 => 0xFF

or (separate sign bit):

-127 => 0x8F

-0 => 0x80

0 => 0x00

127 => 0x7F

The reason computer use the one's complement is that all the others are rather difficult to implement efficiently in logic for either addition and especially subtraction.

Since the code in the "false" branch simply uses the result of the subtraction that should not be a practicat problem but I am wondering whether there is some clever trick behind that or if that is just the useless.

I have seen such code too in the past and simply assumed that the original programmer either didn't think further than his nose, or may have used signed integers before, then in a frenzy to avoid coercion dots, changed them to unsigned without reviewing all the code and noticing the now superfluous positive check. In one or two cases the effective code was in fact in the negative branch of the case which of course never could happen, so that made me scratching my head a bit. :cool:

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.