# PICMicrocontoller Bit MathMethod

## Setting bit number X

Tracy Smith says

Here are a couple of ideas for getting 1<<TestBit
```10 cycles:

2^n:
clrw
btfsc    TestBit,2
movlw   b'11111100'

incf     TestBit,w
btfsc    TestBit,1
iorwf   TestBit,f

incf     TestBit,f

skpnc
swapf   TestBit,f

.........
another way (7 cycles [including the call])

two_to_n:
andlw    7
retlw    1
retlw    2
retlw    4
retlw    8
retlw    0x10
retlw    0x20
retlw    0x40
retlw    0x80

............
or probably more to the point

movf     portb,w

btfss    TestBit,2

btfss    TestBit,0

movlw    b'10000000'
btfss    TestBit,1
movlw   b'00100000'

skpnz
goto    _the_bit_is_set

.lo

```

Kevin Blain says:

```clrf    mask    ; empty the mask

; a file called bit contains the 0 to 7 value of the bit position

incf    bit    ; make it 1 to 8, so decfsz works nicely
bsf    STATUS, C    ; set the carry flag. This will be rotated in 'bit' times

loop:
decfsz    bit, f    ; skip out after 'bit' times.
goto    loop
.....               ; rest of code...

Of course, it would be good to ensure that bit is not greater than 7 when
entering the routine.
```

Scott Dattalo says

```set_bit:

;Get a pointer to the byte containing the bit we want:

rrf   bit_position,w
movwf fsr

rrf   fsr,f
rrf   fsr,w
movwf fsr

; find 1<<(bit_position&7) [note, this could be made 2-cycles
;                           shorter]

movlw  0101b
btfsc  bit_position,0
movlw 1010b

movlw  0011b
btfsc  bit_position,1
movlw 1100b

btfsc  bit_position,2

; now set or clear the bit

iorwf  indf,f         ; Assume we're setting.
btfss  new_state,0
xorwf indf,w         ; assumption was wrong

```

Dmitry says:

```;Probably it would be better to pre allocate bit position value in cell
;shifted by one bit to left, like xxxx.yyyz where xxxx is byte offset
;and yyy bit offset. It let us write more tight code for this case.

set_bit:
swapf   bit_position,W
andlw   0x0F
movwf   FSR

movlw   1
btfsc   bit_position,2  ;y.1
movlw   4

btfsc   bit_position,1  ;y.0

btfsc   bit_position,3  ;y.2

iorwf   INDF,F
btfss   bit_position,0  ;we can use z for this why not?
xorwf   INDF,F
```

Bob Ammerman says:

```To turn on bit "BitNum" of port "PortNum":

movf       PortNum,W
movwf    FSR

movf       BitNum,W
iorwf       INDF,F

To turn off bit "BitNum" of port "PortNum":

movf       PortNum,W
movwf    FSR

movf       BitNum,W
xorlw       0xFF
andwf      INDF,F

; To test bit "BitNum" or port "PortNum"

movf    PortNum,W
movwf FSR
movf    BitNum,W
andwf  INDF,W

; At this point Z is set if PortNum,BitNum was clear
;  Z is clear if PortNum,BitNum was set

retlw    1
retlw    2
retlw    4
retlw    8
retlw    16
retlw    32
retlw    64
retlw    128

```

Code:

• ```; 2003-06-10T09:01:22Z
; test bit #n of working register, Z flag set properly, W left with bit

btfsc   n,0
andlw   0xaa
btfss   n,0
andlw   0x55
btfsc   n,1
andlw   0xcc
btfss   n,1
andlw   0x33
btfsc   n,2
andlw   0xf0
btfss   n,2
andlw   0x0f
```
+

 file: /Techref/microchip/math/bit/setbit.htm, 5KB, , updated: 2003/6/10 12:58, local time: 2022/7/1 04:36, TOP NEW HELP FIND:  3.238.125.76:LOG IN

 ©2022 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?Please DO link to this page! Digg it! / MAKE! Setting bit number X

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
 Did you find what you needed? "No. I'm looking for: " "No. Take me to the search page." "No. Take me to the top so I can drill down by catagory" "No. I'm willing to pay for help, please refer me to a qualified consultant"

### Welcome to sxlist.com!

Site supported by
& kind contributors
just like you!

(here's why

Copies of the site on CD
are available at minimal cost.

.