I'm just learning about bit and byte stuffing. So far, it seems that they're both trying to prevent the same thing from happening in say, HDLC frames, that is, to avoid treating a 0x7E value in the data frame as the 0x7E flag that delimits a frame. The difference is bit stuffing works on a bit-by-bit basis, while byte stuffing works on a byte-by-byte basis. Does this mean that both bit and byte stuffing cannot occur in the same traffic? For example, if I find that the frames contain bit stuffing, then it cannot be byte stuffed as well?
You are correct that bit-stuffing and byte-stuffing perform broadly the same role, but note:
- There is no reason in principle why you cannot do both at the same time, though this is not typically done - rather:
- Bit stuffing is typically used on synchronous serial links.
- Byte stuffing is typically used on asynchronous serial links (eg. RS-232).
- In any case, both ends must agree in advance which convention (bit stuffing, byte stuffing, both, something else) is in force: the sender can't simply choose one, or else the receiver won't know how to interpret the transmissions.
- Bit stuffing is not just to distinguish the inter-frame marker
01111110from the data value 0x7e (which would otherwise also be sent as
01111110) - bits must also be inserted when bits in adjacent bytes might look like the inter-frame marker (as with two bytes
00000111 11100000). This scenario does not arise with byte stuffing.
- Bit stuffing is typically done by the serial hardware; byte stuffing is often done in software. Thus, unless you're directly measuring the electrical signals on the wire, you're unlikely to observe bit stuffing directly - but you may be able to observe byte stuffing via (for example) a debugger.