Bits, bytes, hexadecimal and masks
On the PIC18F4550 almost everything is configured by modifying individual bits inside 8-bit registers (one byte: 2⁸ = 256 states, 0 through 255). To review the digital-systems idea, start with What 8-bit means.
This page complements Basic architecture and Main registers: there you saw what WREG, STATUS, and SFRs are; here you learn how to read, write, and mask bits in assembly.
Mastering binary, hexadecimal, and masks is essential for the exam and lab work (especially Practice 1, which requires logic routines with the ALU).
What is a bit and what is a byte
Section titled “What is a bit and what is a byte”| Concept | Definition | On the PIC18 |
|---|---|---|
| Bit | Smallest unit: 0 or 1 | Each pin, flag, or SFR bit |
| Byte | Group of 8 bits | One register (WREG, LATB, STATUS…) |
| Nibble | Half byte = 4 bits | One hex digit (0–F) or high/low nibble |
Bits are numbered 0 through 7. Bit 0 is the LSB (least significant); bit 7 is the MSB (most significant):
Bit: 7 6 5 4 3 2 1 0Value: 1 1 0 0 0 0 1 1 → decimal 195, hex 0xC3Weight: 128 64 32 16 8 4 2 1 (powers of 2)To get decimal: add the weights where the bit is 1. In the example: 128 + 64 + 2 + 1 = 195.
Hexadecimal — why you use it
Section titled “Hexadecimal — why you use it”Writing B'11000011' is long; in hex one byte is only 2 digits (0xC3). Each hex digit is 4 bits (one nibble):
| Hex | Binary | Decimal |
|---|---|---|
| 0 | 0000 | 0 |
| 1 | 0001 | 1 |
| … | … | … |
| 9 | 1001 | 9 |
| A | 1010 | 10 |
| B | 1011 | 11 |
| C | 1100 | 12 |
| D | 1101 | 13 |
| E | 1110 | 14 |
| F | 1111 | 15 |
Quick conversion table (useful bytes)
Section titled “Quick conversion table (useful bytes)”| Decimal | Binary | Hex (0x) | Hex (H suffix) | Typical use |
|---|---|---|---|---|
| 0 | 00000000 | 0x00 | 00H | All off |
| 1 | 00000001 | 0x01 | 01H | Bit 0 only |
| 15 | 00001111 | 0x0F | 0FH | Full low nibble |
| 240 | 11110000 | 0xF0 | F0H | Full high nibble |
| 170 | 10101010 | 0xAA | AAH | Alternating pattern |
| 255 | 11111111 | 0xFF | FFH | All bits set |
Step-by-step conversion
Section titled “Step-by-step conversion”Decimal → binary: divide by 2 and read remainders bottom to top, or subtract powers of 2 (128, 64, 32…).
Hex → binary: expand each hex digit to 4 bits. 0xC3 → C = 1100, 3 = 0011 → 11000011.
Binary → hex: group in 4 bits from the right. 11000011 → 1100 + 0011 → 0xC3.
Decimal → hex: divide by 16. Example: 195 ÷ 16 = 12 remainder 3 → 0xC3 or C3H.
Equivalent hex notations
Section titled “Equivalent hex notations”The same byte can be written in several ways. All mean the same value:
| Style | Example | Typical use |
|---|---|---|
0x prefix | 0x00, 0xFF, 0xC3 | MPASM code, many tools |
| H suffix | 00H, FFH, C3H | Notes, reports, exercise sheets |
| MPASM h suffix | 00h, 0FFh, 0C3h | Valid alternative in .ASM |
H suffix rule: digits are hex; H at the end means base 16. 00H = zero; 0FH = 15; F0H = 240. Do not confuse 0FH (15) with F0H (240).
A single bit — set, clear, toggle
Section titled “A single bit — set, clear, toggle”When you only need to change one bit of an SFR without touching the others, use bit instructions:
| Instruction | Effect | Example |
|---|---|---|
BSF | Set bit to 1 | BSF LATB, 0 → RB0 high |
BCF | Clear bit to 0 | BCF TRISB, 0 → RB0 as output |
BTFSS | Skip if bit is 1 | BTFSS PORTB, 3 → read button |
BTFSC | Skip if bit is 0 | BTFSC STATUS, Z → skip if Z=1 |
You already saw this in Architecture — SFR and Registers — GPIO:
; Configure RB0 as output and turn LED onBCF TRISB, 0 ; TRISB bit 0 = 0 → outputBSF LATB, 0 ; LATB bit 0 = 1 → LED onTo toggle a bit without reading the whole byte:
; Toggle RB0MOVLW B'00000001' ; mask bit 0 onlyXORWF LATB, F ; XOR bit by bit → toggles that bitBit masks — change several bits at once
Section titled “Bit masks — change several bits at once”A mask is a bit pattern showing which positions you want to affect. ALU logic operations apply the mask bit by bit on WREG and another operand.
AND — clear or filter bits
Section titled “AND — clear or filter bits”AND keeps 1 only where both operands have 1. Use it to mask (keep certain bits) or clear bits by putting 0 in the mask:
; Read only the high nibble of PORTB (bits 7..4)MOVF PORTB, WANDLW B'11110000' ; 0xF0 — bits 4-7 pass; 0-3 become 0
; Clear bits 4-7 of LATC without touching 0-3MOVLW B'00001111' ; mask: keep low nibbleANDWF LATC, F ; high bits → 0IOR (OR) — set bits
Section titled “IOR (OR) — set bits”IOR sets 1 where either operand has 1. Use it to turn on bits without clearing others:
; Turn on bits 0 and 3 of LATB (rest unchanged)MOVLW B'00001001' ; bits 0 and 3 setIORWF LATB, FIn MPASM the instruction is IORWF / IORLW.
XOR — toggle or compare
Section titled “XOR — toggle or compare”XOR yields 1 where bits differ. Uses:
- Toggle selected bits (see above).
- Compare if two values are equal (result 0 → flag Z = 1 in
STATUS).
MOVLW D'5'XORWF counter, W ; if counter == 5 → W = 0 → Z = 1BTFSS STATUS, ZGOTO not_five ; if counter != 5 → jump to destination; counter == 5 ...GOTO cmp_donenot_five: ; counter != 5 ...cmp_done:Complement (NOT)
Section titled “Complement (NOT)”Inverts all bits: 0 → 1, 1 → 0. On PIC18 with WREG:
MOVF LATB, WXORLW B'11111111' ; equivalent to NOT of W (0xFF)MOVWF temp ; temp = complement of LATBOr with COMF on a file register:
COMF temp, F ; temp = NOT tempRotates
Section titled “Rotates”Shift bits with or without carry — Practice 1 asks for rotate without carry:
| Instruction | Direction | What it does |
|---|---|---|
RLCF | Left | MSB goes to C; rotate through carry |
RRCF | Right | LSB goes to C; rotate right |
RLNCF | Left | Rotate without carry — bit that falls off re-enters |
RRNCF | Right | Rotate without carry to the right |
Example — rotate temp left without carry:
RLNCF temp, FHigh and low nibble
Section titled “High and low nibble”Many lab reports ask you to split a byte into two nibbles (displays, BCD, tables):
; temp = 0xC3 → high_nibble = 0xC0, low_nibble = 0x03MOVF temp, WANDLW B'11110000' ; 0xF0 — bits 7..4 onlyMOVWF high_nibble
MOVF temp, WANDLW B'00001111' ; 0x0F — bits 3..0 onlyMOVWF low_nibbleTo swap high and low nibbles:
SWAPF temp, F ; swap high ↔ low nibbleLogic operations and Practice 1
Section titled “Logic operations and Practice 1”The UNEXPO lab requires software routines for add, subtract, multiply, divide, plus OR, AND, XOR, complement, and rotate using the PIC18 ALU. Logic ops usually go through WREG:
; AND between PORTB and a mask in WMOVF PORTB, WANDLW B'00001111' ; W = PORTB AND 0x0F
; OR between LATC and WMOVLW B'10000000'IORWF LATC, F ; LATC = LATC OR W
; XOR between temp and WMOVLW B'10101010'XORWF temp, FSee Architecture — STATUS: after AND, OR, or XOR, check Z (result zero?) and N (sign of bit 7).
Integrated example — configure several pins
Section titled “Integrated example — configure several pins”You want RB0–RB3 as outputs and RB4–RB7 as inputs in one step (mask on TRISB: 1 = input):
MOVLW B'11110000' ; bits 4-7 = 1 (input); 0-3 = 0 (output)MOVWF TRISB ; write full byteOr bit by bit (clearer when learning, longer in code):
; Outputs RB0-RB3BCF TRISB, 0BCF TRISB, 1BCF TRISB, 2BCF TRISB, 3; Inputs RB4-RB7BSF TRISB, 4BSF TRISB, 5BSF TRISB, 6BSF TRISB, 7Ejercicio · Parcial
Convert 0xC3 to binary and explain BCF
0xC3 = 11000011 (decimal 195). Active bits: 0, 1, 6, and 7.
If PORTB was 0xC3 and you run BCF PORTB, 6:
- Only bit 6 becomes 0; the rest unchanged.
- Result:
11000011→10000011= 0x83 (decimal 131).
Why use BCF instead of writing all of PORTB? You avoid accidentally changing pins you did not mean to touch.
Common mistakes
Section titled “Common mistakes”- Mixing notation:
0x0F= 0FH = 15, but0xF0= F0H = 240 — nibble order matters. - Bit vs pin: bit 3 of
LATBis pin RB3; numbering starts at 0. - AND vs OR: AND clears bits where the mask is 0; OR sets bits where the mask is 1.
- Inverted mask on TRIS: in
TRISx, 1 = input and 0 = output — when designing masks, remember what each bit means. - Writing
PORTxfor outputs: writeLATx(orBSF/BCFonLATx); read inputs withPORTx. See Registers — PORT vs LAT. - Forgetting
-LWvs-WF:ANDLWuses a literal →WREG;ANDWFuses a register →WREGor the register (F).
Exam-style questions
Section titled “Exam-style questions”How many distinct unsigned values does one byte represent?
256 values, 0 through 255 (2⁸ combinations).
What does ANDLW 0x0F do after MOVF PORTB, W?
Leaves only the low nibble of PORTB in WREG; bits 4–7 become 0.
What is an OR mask used for?
To set specific bits without clearing those already at 1.
How do you tell if two bytes are equal using XOR?
XORWF between them; if the result is 0, flag Z in STATUS is 1.
Bits and hexadecimal
Based on: Clase Tema 2 y 3 (1).pdf
0 of 0 answered
Sign in with CALETAS to sync this result across devices.
Sign in with CALETAS