Combinator magic help ?

Don't know how to use a machine? Looking for efficient setups? Stuck in a mission?
Post Reply
Ethel
Manual Inserter
Manual Inserter
Posts: 3
Joined: Mon Aug 03, 2020 8:23 am
Contact:

Combinator magic help ?

Post by Ethel »

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 :

Image

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 :)

Serenity
Filter Inserter
Filter Inserter
Posts: 839
Joined: Fri Apr 15, 2016 6:16 am
Contact:

Re: Combinator magic help ?

Post by Serenity »

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

Ethel
Manual Inserter
Manual Inserter
Posts: 3
Joined: Mon Aug 03, 2020 8:23 am
Contact:

Re: Combinator magic help ?

Post by Ethel »

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 !

torne
Filter Inserter
Filter Inserter
Posts: 289
Joined: Sun Jan 01, 2017 11:54 am
Contact:

Re: Combinator magic help ?

Post by torne »

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.

Post Reply

Return to “Gameplay Help”

Who is online

Users browsing this forum: No registered users