user1852232
user1852232

Reputation: 13

Dividing a 16 bit number by 2 [PIC microcontroller]

If a 16 bit value is stored (in a pic microcontroller) as a High byte and Low byte, how do you go about dividing them by 2? How can a 16 bit number be rotated right?

Thanks.

Upvotes: 1

Views: 2457

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726849

Dividing a 16-bit number by two is equivalent to shifting the number right by a single bit*. Clear the carry flag, rotate the higher byte right through the carry, and then rotate the lower byte right through the carry. You can find code for it here:

LSR16   MACRO   VAR16
    BCF     STATUS, C       ; Clear carry
    RRF     (VAR16)+1,F     ; Rotate high byte right
    RRF     (VAR16),F       ; Rotate low byte right
    ENDM


* Of course this is an integer division: when odd numbers are divided by two, the 0.5 is truncated.

Upvotes: 7

Related Questions