Hello everyone! Going back to the game after a long pause, and I have a bit of an issue regarding some combinator magic. Not sure if this is something I dont understand or a bug ?
I use arithmetic combinators to transfer 32 1-bit messages on one combinator signal.
This is a simplified yet functional model of what I am trying to accomplish :
A "0 or 1" signal is left-shifted by a certain value, then mixed up with the rest and decoded by right-shifting the total.
However, it does work only when the 32nd bit is not active. I know this is the sign bit, but I would expect it to be right-shifted nevertheless. Instead, when I use the operation 1 << 31, every signal that I decode is negative. I mean, right shifting the sign bit should put a 0 in its place and transform the value back to a positive integer, right ? Or am I missing something ? I have a work around for that (detect values != 0 instead of > 0) but I dont understand why it behaves this way ?
Thank you for your help
Combinator magic help ?
Re: Combinator magic help ?
Not sure if this applies to Factorio combinators as well (it's ultimately C++ under the hood), but in regular programming right-shifting negative numbers is a huge trap as they are stored in two-complement
Re: Combinator magic help ?
Yes, indeed. That is why the "modulo" operation I have at the end returns -1. And my "workaround" is indeed the solution to my problem. Thanks !
Re: Combinator magic help ?
The right shift in the arithmetic combinator is what's called an "arithmetic right shift" as opposed to a "logical right shift". In an arithmetic shift the sign bit is preserved, which means that if you start with a negative number you will always have a negative number, and all the bits shifted "in" are 1s.
This means that each bit you shift right by is equivalent to dividing by 2 (with rounding toward negative infinity) for both positive and negative numbers, instead of having a very different kind of result depending on whether the initial number is positive or negative.
This means that each bit you shift right by is equivalent to dividing by 2 (with rounding toward negative infinity) for both positive and negative numbers, instead of having a very different kind of result depending on whether the initial number is positive or negative.