Like this: http://www.mathcs.emory.edu/~cheung/Cou ... array.html

But based on unsigned char instead of int.

Code: Select all

```
void SetBit( unsigned char A[ ], int k )
{
int i = k/8;
int pos = k%8;
unsigned int flag = 1; // flag = 0000.....00001
flag = flag << pos; // flag = 0000...010...000 (shifted k positions)
A[i] = A[i] | flag; // Set the bit at the k-th position in A[i]
}
int TestBit( unsigned char A[ ], int k )
{
int i = k/8;
int pos = k%8;
unsigned int flag = 1; // flag = 0000.....00001
flag = flag << pos; // flag = 0000...010...000 (shifted k positions)
if ( A[i] & flag ) // Test the bit at the k-th position in A[i]
return 1;
else
return 0;
}
```

Note: I'm using http://skilldrick.github.io/easy6502/ to test my code and this is why my array is supposed to be in $200 (I choose this at random) in the real code this will be parametrized obviously.

Code: Select all

```
LDA #$15
JSR calculAY
JSR setbit
LDA #$15
JSR calculAY
JSR testbit
BRK
; computes the binary mask to apply/test in A and the array offset in Y from the value in A
calculAY:
STA $00
LSR ; divide by 8
LSR
LSR
TAY ; offset in array
ASL ; multiply by 8
ASL
ASL
STA $01 ; store this value
LDA $00 ; get back the original value
SEC
SBC $01 ; subtract
TAX ; this is the remainder of the division by 8
BEQ nul_mod
LDA #$01
loop: ; move this to compute the 1 bit mask
ASL
DEX
BNE loop
LSR
RTS
nul_mod:
LDA #$01 ; if the remainder was null we have to test the first bit (ie bit 0)
RTS
; set in memory, array starting at $200
setbit:
ORA $200,Y
STA $200,Y
RTS
; test in memory, array starting at $200 result in A (0 or 1)
testbit:
LDA $200,Y
STA $00
BIT $00
BNE ok
LDA #$00
BRK
ok:
LDA #$01
RTS
```