Ekundayo Green
Ekundayo Green

Reputation: 9

Store zeros from ints and use them later

I have 3 sensors that each provide either 0 or 1 (repeatedly in a loop). They are stored individually as int variables. These are then printed using the following:

print ("%d%d%d", Sensor1, Sensor2, Sensor3);

I want to store each combination (ex: 010, 001, 110, etc.) temporarily so that I can use it do something else (I want to have a switch or something eventually where I can do a different operation depending on the value of the sensor combination). I can't store it as an int since that drops the 0s in front.

How can I store these combinations?

Upvotes: 0

Views: 88

Answers (5)

Clifford
Clifford

Reputation: 93476

While the leading zeroes of an integer are not displayed when printed, that does not mean they are "dropped"; they are merely implicit - that is a matter of the format specifier used in teh output of the value rather than the zeros not being present. An int is always a fixed number of binary digits.

Consider:

uint32_t sensor_state = (sensor3 << 3) | (sensor2 << 1) | sensor1 ;

Note that uint32_t is a type alias for an unsigned integer 32 bits in length. It is defined by including the <stdint.h> header file. In this case a plain int would work, but when you are dealing with data at the bit level it is good to be explicit (and unsigned). Here of course a uint8_t would work too, and if your target is an 8 bit device, I suggest you use that.

Here sensor_state is a binary combination of the three sensor values and will have one of the following values:

Sensors        sensor_state
3 2 1     binary  decimal   hexadecimal
---------------------------------------
0 0 0     0000      0         0x00
0 0 1     0001      1         0x01
0 1 0     0010      2         0x02
0 1 1     0011      3         0x03
1 0 0     0100      4         0x04
1 0 1     0101      5         0x05
1 1 0     0110      6         0x06
1 1 1     0111      7         0x07

So you can switch on any combination:

switch( sensor_state )
{
    case 0x00 :
        ...
    break ;

    case 0x01 :
        ...
    break ;

    case 0x02 :
        ...
    break ;

    ...

    case 0x07 :
        ...
    break ;

    default :
        // unexpected invalid combination
    break ;
}

You might usefully create an enumeration for each combination:

enum eSensorStates
{
    NO_SENSOR = 0,
    SENSOR1,
    SENSOR2,
    SENSOR12,
    SENSOR3,
    SENSOR13,
    SENSOR23,
    SENSOR123
}

Then you can write:

switch( sensor_state )
{
    case NO_SENSOR :
        ...
    break ;

    case SENSOR1:
        ...
    break ;

    case SENSOR2:
        ...
    break ;

    ...

    case SENSOR123 :
        ...
    break ;

    default :
        // unexpected invalid combination
    break ;
}

You may of course use enumeration names that make specific sense in your application - that reflect the meaning or action for each combination rather than the generic names I have chosen.

Upvotes: 0

hyde
hyde

Reputation: 62797

So you have

int Sensor1, Sensor2, Sensor3;
// have code to initialize above variables to 0 or 1

To store these as one integer in base 10, assuming they really all are 0 or 1, you can do:

int Sensors_10 = Sensor1 * 100 + Sensor2 * 10 + Sensor3;

And then to get them back:

Sensor1 = Sensors_10 / 100 % 10;
Sensor2 = Sensors_10 / 10 % 10;
Sensor3 = Sensors_10 % 10;

Obviously order of sensors can be whatever, as long as it matches between packing and unpacking.


But, you only need 1 bit to store each sensor, so could use binary:

int Sensors_2 = Sensor1 * 4 + Sensor2 * 2 + Sensor3;

...

Sensor1 = Sensors_2 / 4 % 2;
Sensor2 = Sensors_2 / 4 % 2;
Sensor3 = Sensors_2 % 2;

But, with computer binary numbers are special, so the binary version is more commonly written like this:

int Sensors_2 = Sensor1 << 2 | Sensor2 << 1 | Sensor3;

...

Sensor1 = Sensors_2 >> 2 & 1;
Sensor2 = Sensors_2 >> 1 & 1;
Sensor3 = Sensors_2 & 1;

Where |, <<, >> and & are bitwise OR, shift and AND operators, and explaining what they do is beyond scope of this question, but one note about them: When there are no "overlapping" one-bits and numbers are positive, then result of | is same as result of +.

Answer of haccks covers how to make C compiler do this for you, without doing your own bit manipulation.


To print Sensors_10 with leading zeros, you can do printf("%03d", Sensors_10);. C standard library does not have a way to print binary numbers directly, so you need your own code to print the bits one-by-one, so you might as well printf("%d%d%d", Sensor1, Sensor2, Sensor3); then.

Upvotes: 1

Asis
Asis

Reputation: 733

If you are using a linux environment then by using the command you can easily save the output that are displayed in your console.

Let here sensor.c be your source file Then,

$ gcc -o a sensor.c
$ ./a > senser.txt

Then you have a .txt file with all output stored in a txt file. And these can be again used as inputs in your other.c files like :

$ gcc -o other other.c
$ ./other < senser.txt

If you want to store those sensor1,sensor2,sensor3 internally and use internally then you can simply use the arrays or Structure like :

main(){
   int Sensor[1][3];
   Sensor[0][0] = 0;
   Sensor[0][1] = 1;
   Sensor[0][2] = 0;  

   print ("%d%d%d", Sensor[0][0], Sensor[0][1], Sensor[0][2]);  

}

Upvotes: 0

ani627
ani627

Reputation: 6057

You can use a 2D int array to store the values and use it later.

E.g int sen_value[1000][3]; use it in the loop to store the values.

Example how you can use it in loop:

#include <stdio.h>
int main ()
{
    int i;
    int sen_value[10][3];
    for(i=0;i<10;i++)
    {
        //Assigning the values
        sen_value[i][0] = 0;
        sen_value[i][1] = 0;
        sen_value[i][2] = 0;

        //Use the way you want
        printf("%d %d %d\n",sen_value[i][0],sen_value[i][1],sen_value[i][2]);
    }
    return 0;
}

Or you can use it just once and then reset it after each operation, For example:

#include <stdio.h>
int main ()
{
    int sen_value[1][3];
    //Assigning the values
    sen_value[0][0] = 0;
    sen_value[0][1] = 0;
    sen_value[0][2] = 0;

    //Use the way you want
    printf("%d %d %d\n",sen_value[0][0],sen_value[0][1],sen_value[0][2]);
    return 0;
}

Upvotes: 0

haccks
haccks

Reputation: 106012

You can use structure bit field for this.

struct Bit{
    bool Sensor1 : 1;
    bool Sensor2 : 1;
    bool Sensor3 : 1;
};

int main(void)
{
    struct Bit bit = {0, 1, 0};
    printf ("%d%d%d", bit.Sensor1, bit.Sensor2, bit.Sensor3);
}

Upvotes: 1

Related Questions