Binary decoder

Post pictures and videos of your factories.
If possible, please post also the blueprints/maps of your creations!
For art/design etc. you can go to Fan Art.

jo2k
Burner Inserter
Burner Inserter
Posts: 11
Joined: Wed Oct 05, 2016 8:04 pm
Contact:

Re: Binary decoder

Post by jo2k »

This combinator stuff is very nice. Especially if you use them with the knowledge of the integer overflow quirk.
Large displays with 1 combinator per row are really easy now. Yeah. :-)

For those, who hasn't understood how it works (like me), I wrote this little tool (C code, 32bit int, q&d) that helped me to open my eyes.
Thanks, XKnight.

Code: Select all

#include <stdio.h>

int main()
{
    int c = 1;
    int i = 0;

    int in = 0;
    int tmp = 0;
    int bit = 0;

    do
    {
        printf("In: ");
        scanf("%d", &in);
        if(in == 0) break;

        c = 1;
        for(i = 32; i > 0; i--)
        {
            tmp = c * in;
            bit = (tmp < 0);
            printf("%2d: %11d -> %11d = %d\n", i, c, tmp, bit);
            c = c * 2;
        }
    } while(1);

    return 0;
}
@Amegatron: I hope you found your mistake in the meantime, because -2147483647 isn't the correct number. If you use the correct factor for every bit, then you will never get an incorrect decoding. ;-)
User avatar
ShinoHarvest
Long Handed Inserter
Long Handed Inserter
Posts: 50
Joined: Tue May 08, 2018 3:29 pm
Contact:

Re: Binary decoder

Post by ShinoHarvest »

in solo game 2147483647 + 1 = 2147483648
dedicated serv 2147483647+1 = -2147483648

>_<
frequentPhoenix
Manual Inserter
Manual Inserter
Posts: 2
Joined: Wed Aug 23, 2023 8:55 pm
Contact:

Re: Binary decoder

Post by frequentPhoenix »

I have tried and failed to make XKnight's decoder. Can anyone post non-cryptic information for the rest of us?
Tertius
Filter Inserter
Filter Inserter
Posts: 918
Joined: Fri Mar 19, 2021 5:58 pm
Contact:

Re: Binary decoder

Post by Tertius »

In the thread below, there is an extended discussion about bit manipulation and a binary decoder is developed by the people, essentially duplicating some discussion from this thread and developing the same decoder in the end. The advantage of that thread is there survived many images and there are a lot of blueprints.
I link my post that contains some kind of conclusion and the decoder you're asking about. Scroll backwards and you have the whole discussion.

About the blueprint in that post: Ignore the rightmost 2 combinators, they're only for producing the visualization of the two's complement. The actual decoder for the number is only the 2 constant combinators with the left arithmethic combinator, as mentioned in this thread, and extended to 32-bit.
In case it isn't clear: inspect not only the combinator, inspect the conditions in all the lamps as well. They're essential.

viewtopic.php?p=557512#p557512
Nidan
Filter Inserter
Filter Inserter
Posts: 267
Joined: Sat Nov 21, 2015 1:40 am
Contact:

Re: Binary decoder

Post by Nidan »

frequentPhoenix wrote: Wed Aug 23, 2023 9:21 pm I have tried and failed to make XKnight's decoder. Can anyone post non-cryptic information for the rest of us?
Those posts are quite old. Since then combinators gained the ability to do bitwise logic, making the old designs obsolete unless you want to learn the trickery they used. In current factorio a combinator set to AND will do.
Aikahona
Manual Inserter
Manual Inserter
Posts: 1
Joined: Thu Jul 18, 2024 3:58 am
Contact:

Re: Binary decoder

Post by Aikahona »

Because the manual links to this forum topic, and because there isn't any modern useful info in this thread, see this:

You can simply decode the locomotive or wagon positions using a single constant combinator and a single arithmetic combinator as follows.

In the constant combinator, put a (1) signal (signal tab on the constant combinator) and give it the value 1. Add a (2) signal as 2. Add a (3) signal as 4, a (4) signal as 8, (5) as 16, (6) = 32, (7) =64, and so on for as much rolling stock as your train has. You will have noticed that each signal is double the value of the previous signal. If for example your longest train you plan to use is a LCCCCLCCCCL (where L is a locomotive, and C is a cargo wagon), you will need to 10 signals, in which case go alpha-numeric where you use 1-9 then A-Z. So in this case set (A) = 512. Then just blueprint this combinator for easy later use.

Image

Now link the constant combinator with the logistic wire colour of your choice to the LTN train stop output, the constant combinator and the arithmetic combinator input.

Set the arithmetic combinator to (Each) AND (encoded position of <something> - be that a locomotive if you're planning a refueling stop, artillery cannon if you're planning to replenish ammo, etc). Set the Output to (Each).

Image

Now use any colour logistic wire to connect the arithmetic combinator output to your inserter(s), and set the inserter to Enable/disable (default) and (1) > 0 as the condition. If you prefer, you could also set it to (1) ≠ 0. That's the 'not equal' symbol. Either works.

Image

Likewise if you have a refueling inserter at all the other positions and are filtering on (encoded positions of every locomotive), set each inserter on your fuel conveyor belt or chests sequentially for example (2) > 0 then (3) > 0 and so on.

If you wanted a multi-function stop which handled refueling and wagon processing just link another arithmetic combinator up with the same colour logistic wire from the constant combinator, but use (Each) AND (encode postions of every cargo wagon) this time. Then connect a separate set of inserters to the output of that arithmetic combinator to your goods conveyor belts or chests.

NB: Programmers like to use the numbers (0-9 and A-Z) where the first position is (0) because in many computer languages that is the default for array indexes but that's really irrelevant here and you can use whatever makes sense to you. Most non-programmers will normally find 1-9 + A-Z easier to work with.
Tertius
Filter Inserter
Filter Inserter
Posts: 918
Joined: Fri Mar 19, 2021 5:58 pm
Contact:

Re: Binary decoder

Post by Tertius »

Aikahona wrote: Thu Jul 18, 2024 5:12 am NB: Programmers like to use the numbers (0-9 and A-Z) where the first position is (0) because in many computer languages that is the default for array indexes
It's not really because 0 as first array index, it's more that the signal name could represent the exponent for base 2. All values are powers of 2, so it's only natural to indicate the exponent with the signal name. It's really giving me a twinge when you start the zeroth bit and 2^0 as signal 1.
Post Reply

Return to “Show your Creations”