[futurebasic] bit twiddling

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : February 1999 : Group Archive : Group : All Groups

From: "Edwards, Waverly" <WEdwards@...>
Date: Thu, 4 Feb 1999 17:30:48 -0500
I'm seriously hoping someone has an idea.  I've been play with bits and I
have the strangest problem. This function clears an array of records and
sets just one array subscript.

The problem that I have is that if you look at the lines where I use BITSET
AND BITCLEAR this function works flawlessly.  If I set the variable
cellState% using a bitwise OR, or if I set it with XOR I get different
results.  cellState% value does not remain constant.  g.gOffsetArrayH& is an
XREF'ed array of records.  

The reason why I want to use OR and XOR is for two reasons, I want to set
multiple bits withought having to using BITSET/BITLCEAR multiple times and I
also want speed up my program by relieving myself of the overhead of using
the toolbox.  I may loop through this array 30,000 times just to clear all
the cells but one.  This function *is* used heavily.

I have other functions that rely on the same use of BITSET/BITCLEAR.  I'm
only dealing with this one because once this one is fixed, I can apply the
same fix to the others.

In order to make the program work I only need to comment out the line using
OR/XOR and use BITSET/BITCLEAR.

Last question.  Anyone have ideas on general handling and BIT manipulation
on byte variables.  I was originally trying to use byte variables but this
proved to be a bit tricky and it just was easier in the end to use an
integer variable.

I tried to provide as much info and code as possible without going
overboard.

Hopefully, someone has some ideas.


Thanks,


W.


DIM RECORD cellGlo
DIM cOffSet&
DIM cState%
DIM END RECORD _cellRecSz

END GLOBALS


CLEAR LOCAL
LOCAL FN addOffsetArray

g.OffsetArrayH& = FN NEWHANDLE _clear ((_maxList + 1) *(_cellRecSz)
osErr% = FN HNOPURGE(g.OffsetArrayH&)

END FN

'CLEAR LOCAL
LOCAL FN cellSelOne(selCell%)
  DIM cell&,cellState%,statePtr&
  DIM cellNum%,numStrs&,hilState%
  
  cell&  = g.gOffsetArrayH&
  XREF@ cell.cellRecSz(_maxCells)
  numStrs&    = FN countStr(g.gListH&)
  cellNum% = 0
  
  LONG IF selCell% => _minCells AND selCell% <= numStrs&'protect against
self, make sure its within range
    
    WHILE cellNum% < numStrs&
      INC(cellNum%)                               'start with one, always
      cellState% = cell.cState%(cellNum%)
      hilState%  = (cellState% AND _cellSelectedBit%)
      LONG IF (cellState% AND _cellDisabledBit%) = _false 'dont touch if
disabled
        'CALL BITCLR(cellState%, _bitFldOffset - _cellSelectedBit)
        cellState% = (cellState% XOR _cellSelectedBit%) 
        LONG IF hilState% <> (cellState% AND _cellSelectedBit%)
          CALL BITSET(cellState%,_bitFldOffset - _cellStateChangeBit)'need
to change to bitwise OR
        END IF
        cell.cState%(cellNum%) = cellState%
      END IF
    WEND
    
    cellState% = cell.cState%(selCell%)
    hilState%  = (cellState% AND _cellSelectedBit%)
    LONG IF (cellState% AND _cellDisabledBit%) = _false
      'LONG IF FN BITTST(cellState%,_bitFldOffset - _cellDisabledBit) =
_false'dont touch if disabled
      'CALL BITSET(cellState%, _bitFldOffset - _cellSelectedBit)
      cellState% = (cellState% OR _cellSelectedBit%) 
      LONG IF hilState% <> (cellState% AND _cellSelectedBit%)
        CALL BITSET(cellState%,_bitFldOffset - _cellStateChangeBit)
      END IF
      cell.cState%(selCell%) = cellState%
    END IF
  END IF
END FN