---
title: "8085 ALP: interchange two bits of bytes"
subject: "Microprocessor"
chapter: 2
key: "semester_4/microprocessor:alp-8085-interchange-bits"
source: https://www.bctengineer.com/q/semester_4/microprocessor/alp-8085-interchange-bits
site: BCT Engineering
---

# 8085 ALP: interchange two bits of bytes

Microprocessor · Chapter 2: Programming with 8085 Microprocessor

- Asked in: 2075 Baisakh, 6 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_4/microprocessor/alp-8085-interchange-bits
- Whole chapter: https://www.bctengineer.com/md/semester_4/microprocessor/chapter-2

## Question

Write an ALP in 8085 to transfer 20 bytes of data in a table to another table by interchanging D1 and D4 bits of each byte.

## Short answer

The bits are swapped only if D1 and D4 differ. The byte is rotated left 3 times to align D1 with D4; XOR with original gives D1 xor D4 at D4. Mask 10H, shift back to D2 (02H), OR with 10H to form toggle mask 12H if different or 00H if same; XOR with original to swap. 20 bytes transferred.

## Answer

**Assumptions:** source table at 2000H (20 bytes), destination table at 2100H. Bit numbering D7 ... D0.

**Method:** D1 and D4 are exchanged only when they differ; in that case both bits are toggled. The byte is rotated left 3 times so that D1 lines up with D4; XOR with the original byte gives (D1 xor D4) in position D4; masking and shifting back builds the toggle mask 12H (or 00H); XOR with the original byte completes the swap.


```asm
        LXI H,2000H     ; HL -> source table (20 bytes)
        MVI D,14H       ; counter = 20 decimal
LOOP:   MOV A,M         ; A = byte
        MOV B,A         ; keep a copy of original byte in B
        RLC             ; rotate left 3 times: original D1 comes to D4
        RLC
        RLC
        XRA B           ; bit D4 of A = (D1 xor D4) of original
        ANI 10H         ; keep only D4: 10H if D1 and D4 differ, else 00H
        MOV C,A         ; C = 10H or 00H
        RRC             ; move the difference flag to D1 (10H -> 02H)
        RRC
        RRC
        ORA C           ; A = 12H if bits differ, 00H if same
        XRA B           ; toggle D1 and D4 only when they differ = swap
        INR H           ; destination is source + 0100H (2100H)
        MOV M,A         ; store swapped byte in table 2
        DCR H           ; back to source table
        INX H           ; next byte
        DCR D           ; one byte done
        JNZ LOOP        ; repeat for all 20 bytes
        HLT
```


**Check:** byte 10H (D4 = 1, D1 = 0) gives a mask of 12H, so 10H xor 12H = 02H, which is the exchanged result. A byte like 12H (both 1) gives mask 00H and stays 12H.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What idea does the program use to exchange bits D1 and D4?
   A. It always toggles both D1 and D4 using XRI 12H
   B. It clears both bits and then sets them again from the original byte
   C. If D1 and D4 differ, both are toggled; if they are the same, the byte is left unchanged
   D. It rotates the whole byte right 3 times so that D4 becomes D1
   Answer: C. If the two bits are equal, swapping changes nothing. If they differ, swapping is the same as toggling both, done with XOR mask 12H. (Key point: Swapping D1 and D4 = toggling both only when they differ)

2. Why does the program rotate the byte left 3 times before XRA B?
   A. To bring original D1 into the D4 position so XOR gives (D1 xor D4) at D4
   B. To multiply the byte by 8
   C. To move D4 into the carry flag
   D. To clear the lower three bits of the byte
   Answer: A. D1 is 3 places below D4, so three left rotations put it at D4. XOR with the original byte then shows whether the two bits differ. (Key point: RLC 3 times lines D1 up with D4 to compare them by XOR)

3. The source byte is 10H (D4 = 1, D1 = 0). What is stored in the destination table?
   A. 10H
   B. 12H
   C. 00H
   D. 02H
   Answer: D. The bits differ, so the mask is 12H, and 10H xor 12H = 02H: D4 is now 0 and D1 is 1. (Key point: Toggle mask 12H: 10H becomes 02H)

## Related in Microprocessor

- [8085 ALP: divide two bytes](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-divide-bytes) — Microprocessor, chapter 2
- [8085 ALP: exchange bits D6 and D2](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-exchange-d6-d2) — Microprocessor, chapter 2
- [8085 ALP: transfer data with more than 3 ones](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-ones-greater-than-3) — Microprocessor, chapter 2
- [8085 ALP: odd parity transfer with bit change](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-odd-parity-transfer) — Microprocessor, chapter 2
- [8085 ALP: compare two tables of 16-bit data](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-compare-tables-1ffffh) — Microprocessor, chapter 2
- [8085 ALP: transfer with bit set/reset](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Aalp-8085-set-reset-bit-80h) — Microprocessor, chapter 2

## Related in other subjects

- [Interfacing: 512-byte RAM with 8085](https://www.bctengineer.com/question.md?key=semester_5%2Finstrumentation_ii%3Arag-ram-512-interface) — Instrumentation II, chapter 2
- [Double handshake data transfer with 8255-PPI](https://www.bctengineer.com/question.md?key=semester_5%2Finstrumentation_ii%3Adouble-handshake-8255) — Instrumentation II, chapter 2
- [Two-inertia rotational system transfer function and T-I analogy](https://www.bctengineer.com/question.md?key=semester_3%2Fcontrol_systems%3Atf-rot-j1-j2-k) — Control Systems, chapter 2
