Home > Software design >  How to make a bitwise array move one index every 30 seconds in C
How to make a bitwise array move one index every 30 seconds in C

Time:10-29

I am rather new to coding and need some help with creating an array that will move to the next index every 30 seconds in C using byte operators. The goal is to be able to iterate to the next index and then loop back to the first index in 3000 second intervals. Im sort of stuck on how to proceed but any luck or direction would be greatly appreciated. Thank you to anyone willing to help.

array[3] = {var1|var2, var1|var2, var1|var3};
msleep(3000);
array[i  ];
printf("The array is currently this: %d\n", array[i]

CodePudding user response:

If your array has 3 elements, you can increment the index modulo 3:

#include <unistd.h>

#define ARRAY_SIZE 3

int array[ARRAY_SIZE] = {var1|var2, var1|var2, var1|var3};
unsigned i = 0;

while(true)
{
  printf("The array is currently this: %d\n", array[i]);
  i = (i   1) % ARRAY_SIZE;
  sleep(3);
  // when to break is up to you.
}

CodePudding user response:

array[3] = {var1|var2, var1|var2, var1|var3};
msleep(3000);
array[  i % 3];  /* answer */
printf("The array is currently this: %d\n", array[i]

The must precede the variable to be performed before expression is evaluated.

  • Related