gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > doing some bitwise operations using a structure

#19701 - slboytoy - Fri Apr 23, 2004 6:15 pm

hi all. i'm working on a logc analyzer display function which checks the bits withen a pair of integers which are stored in a structure (channels.hi and channels.lo). there seems to be a problem when we're trying to switch from 1 int to the other. this results in an output which seems to be missing one bit of data. instead, a blank space is drawn instead of an actual piece of data.

anybody see what we are missing in this chunk of code?
Code:
   
do
{
                //Drawing the horizontal line
   if(count > 31)
   {
      if(channels.hi & (1 << count - 31))
      {
         y = yHigh;
      }
      else
      {
         y = yLow;
      }
      x = HorizontalLines(x, y, X_Length);      //Returns new value of 'x' cordinites
   }
   else
       {
                       if(channels.lo & (1 << count))
      {
         y = yHigh;
      }
      else
      {
         y = yLow;
      }
      x = HorizontalLines(x, y, X_Length);      //Returns new value of 'x' cordinites
   }

   //Checks when to put a vertical line
   if (count !=0)
   {
      if(count == 31)
      {
         if (channels.hi & (1 << count -1) && (y == yLow))   //Low to High
         {
            VerticalLines(x, yLow, yHigh);
         }
         else if (channels.hi & (1 << count -1) && (y == yHigh))      //Still High
         {}
         else if (y == yHigh )                  //High to Low
                             {
            VerticalLines(x, yLow, yHigh);
         }
      }
      if(count > 31)
      {
         if (channels.hi & (1 << count -32) && (y == yLow))   //Low to High
         {
            VerticalLines(x, yLow, yHigh);
         }
         else if (channels.hi & (1 << count -32) && (y == yHigh))      //Still High
         {}
         else if (y == yHigh )                  //High to Low
         {
            VerticalLines(x, yLow, yHigh);
         }
      }

      else
      {
         if (channels.lo & (1 << count -1) && (y == yLow))   //Low to High
         {
            VerticalLines(x, yLow, yHigh);
         }
         else if (channels.lo & (1 << count -1) && (y == yHigh))   //Still High
         {}
         else if (y == yHigh )                     //High to Low
         {
            VerticalLines(x, yLow, yHigh);
         }
      }

   }

   count--;

}while(count > 0);

#19702 - sajiimori - Fri Apr 23, 2004 6:21 pm

Yeah, parentheses. The bit shift operators are higher priority than addition and subtraction.

#19703 - slboytoy - Fri Apr 23, 2004 6:29 pm

tried that out. it still does the same thing. if you think it would be easier to understand if i provided the source code for testing i'll post it later.

#19711 - sajiimori - Fri Apr 23, 2004 7:50 pm

You're going to have to post the new version because I don't know what you changed.

Anyway, I don't know what your logic is supposed to be. I'm just seeing a big block of code with a lot of redundant braces in it.

#19718 - sajiimori - Fri Apr 23, 2004 8:28 pm

Does this capture what you were looking for?
Code:

// renamed 'count' to 'bit'

for(; bit > 0; --bit)
{
  int word = bit > 31 ? channels.hi : channels.lo;
  int old_y = y;
  y = word & (1 << (bit & 31)) ? yHigh : yLow;
  x = HorizontalLines(x, y, X_Length);

  if(bit != 0 && y != old_y)
    VerticalLines(x, yLow, yHigh);
}