Software - Power Of 2 Test

The following code leaves the Z flag set (i.e beq branches) if A is 0 or a power of 2. A is preserved.

```
sta temp
dec temp
bit temp
```

The following code leaves the Z flag set (i.e beq branches) if A is one less than a power of 2. A is preserved.

```
sta temp
inc temp
bit temp
```

The following code leaves the Z flag (and the carry) set if A is 256 - a power of 2. In other words, if the upper U bits are 1 and the lower 8-U bits are 0.

```
sta temp
dec temp
ora temp
cmp #$FF
```

The following code leaves the Z flag (and the carry) set if A is 255 or 255 - a power of 2. In other words, if one or fewer bits is clear.

```
sta temp
inc temp
ora temp
cmp #$FF
```

The following code leaves only bit L of the accumulator set, where L is the least significant bit of the accumulator that was originally set.

```
sta temp
eor #$FF ; calculate -A
clc
adc #1
and temp ; A = A & (-A)
```

page revision: 0, last edited: 25 Aug 2010 02:01