# PICMicrocontoller Bit MathMethod

## Converting a 2-bit number to a 4-bit mask

From: Mike Keitz. There is a trick method that works for 4 bits. It replaces a 2-bit number in a variable with a 4-bit mask:

```        incf    BitP, W  ;W = 0001 0010 0011 0100
btfsc   BitP, 1  ;If 0 or 1, result is almost correct now.
iorwf   BitP, F  ;BitP = [0000] [0001] 0011 0111
incf    BitP, F  ;BitP = 0001 0010 0100 1000
```

Jinx says:

Bob Ammerman gave me this one. I used it to convert a device number to a pin bit number. I call it "bobbit" ;-)
```         movf    portb,w
andlw   b'00000011'
movwf   temp1

movlw   .1
btfsc   temp1,1
movlw   .4
btfsc   temp1,0
btfsc   temp1,2
swapf   wreg,w
movwf   temp2

portb = xxxxxx00, temp2 = 00000001
portb = xxxxxx01, temp2 = 00000010
portb = xxxxxx10, temp2 = 00000100
portb = xxxxxx11, temp2 = 00001000

```

Russell McMahon says:

Here's a lateral thought :-).

If you can decide which of the 4 output lines is output 1,2,3,4 (as will usually be the case) you can solve this as follows Assumes bits are in LSBs and no other bits set. AND high bits if required.

Input in W in each case

```   Add 1 to W
If result = 3 then result = 8
```

Or

```   If W = 2 then W = 7
```

 file: /Techref/microchip/math/bit/2to4mask.htm, 1KB, , updated: 2004/12/16 16:18, local time: 2017/12/18 08:58, TOP NEW HELP FIND:  54.91.171.137:LOG IN

 ©2017 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! /  Converting a 2-bit number to a 4-bit mask

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 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" "No. But I'm interested. me at when this page is expanded."

 PICList 2017 contributors: o List host: MIT, Site host massmind.org, Top posters @20171218 RussellMc, Van Horn, David, Sean Breheny, James Cameron, alan.b.pearce, IVP, Neil, Bob Blick, David C Brown, John Gardner, * Page Editors: James Newton, David Cary, and YOU! * Roman Black of Black Robotics donates from sales of Linistep stepper controller kits. * Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters. * Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated! * Contributors: Richard Seriani, Sr.

.