Programming Game of Life in C - Issue with bitwise operations -


i'm trying program conway's game of life in c stumped how store alive or dead cell.

the board stored in array of 32 unsigned longs (32x32 board) each bit representing cell (1 = alive, 0 = dead). can't change design.

so far, have code determining how many neighbors particular cell has need change state based on game's rules (a 1 may need become either 0 or 1, 0 may need either 1 or 0).

i assume can use bitwise operations (|, &, ^) don't know how isolate particular bit in row , store iterate through rest of row , store new, re-calculate row 1 unsigned long.

i.e. if 10011...0 needs 01101...1. how can this?

i realize code need additional loops trying wrap head around particular problem before proceeding.

any appreciated.

#include <stdio.h> #include <stdlib.h> unsigned long columnmask; int compass = 0; int totaliveneighbors = 0; int iterator = 0; int iterator2 = 0;  #define array_size 32 #define neighbors 8  unsigned long grid[array_size]; unsigned long copygrid[array_size]; unsigned long neighbors[neighbors]; unsigned long init32; unsigned long holder = 0;  int main(void){     srand(time(null));     printf("\n");      /** seeds grid random numbers **/     for(iterator = 0; iterator < 32; iterator++){         init32 = ((double)rand()/rand_max)*0xffffffff;         grid[iterator] = init32;     }      /** displays binary representation of grid elements **/     for(iterator = 0; iterator < 32; iterator++){           displaybinary(grid[iterator]);              printf("\n");     }     printf("\n");      /** calculate , sum neighbors 'x' cell **/     /** need iterate through each column shifting mask **/     /** need iterate through each row **/             iterator= 0; //example use             neighbors[0] = north(iterator);             neighbors[1] = south(iterator);             neighbors[2] = east(iterator);             neighbors[3] = west(iterator);             neighbors[4] = northwest(iterator);             neighbors[5] = northeast(iterator);             neighbors[6] = southwest(iterator);             neighbors[7] = southeast(iterator);              columnmask = 0x80000000//need shift iterator value later on             for(compass =0; compass < 8; compass++){                 totaliveneighbors += ((columnmask & neighbors[compass])?1:0);             }  }//end main  void displaybinary(unsigned long x){     unsigned long mask = 0x80000000;     {         //printf("%c",(x & mask)?'x':0x20);         printf("%s", (x & mask)?"1":"0");     } while ((mask >>=1)!=0); }  unsigned long north(int rownum){     if(rownum == 0){         return 0;     }     else             return grid[rownum-1]; }  unsigned long west(int rownum){     holder = grid[rownum] >>1;     return holder; }  unsigned long east(int rownum){     holder = grid[rownum] <<1;     return holder; }  unsigned long south(int rownum){     if(rownum == 31)         return 0;     else             return grid[rownum+1]; }  unsigned long northwest(int rownum){     if(rownum == 0)         return 0;     else{         holder = grid[rownum-1] >>1;         return holder;     } }  unsigned long northeast(int rownum){     if(rownum == 0)         return 0;     else{         holder = grid[rownum-1] <<1;         return holder;     }     }  unsigned long southwest(int rownum){     if(rownum == 31)         return 0;     else{         holder = grid[rownum+1] >>1;         return holder;     } }  unsigned long southeast(int rownum){     if(rownum == 31)         return 0;     else{         holder = grid[rownum+1] <<1;         return holder;     }  } 

you can set bit oring (|) value has bit set.

you can unset bit anding (&) value has every bit set except one.

you can turn value has 1 bit set value has every bit except 1 set not (~) operator.

you can tell if bit set anding (&) value has bit set, , seeing whether result true or false.

you can make value nth bit (counting right, rightmost bit named 0th bit) set left-shifting (<<) value 1 n places.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -