Packing Bits into Opcode Bytes
Assembly manuals describe an instruction as a field diagram: so many bits for the opcode, a bit for the direction flag, two more for the addressing mode. Assemblers, disassemblers and hex editors then show you the finished byte in base-16. This page closes that gap — write the bit pattern you worked out on paper and get the hex byte a listing would print.
11010110 splits into 1101 and 0110, which is D6.Who Needs the Byte Back in Hex
Hand-Assembling an Instruction
Computer Architecture Labs
The same route serves anyone building a value out of flags — a checksum byte, a status word, a bit-packed configuration field — where the design happens in bits and the artefact ships in hex.
From Bit String to Hex Byte
Pad the pattern to whole bytes
Write the leading zeros yourself if your field layout is shorter than eight bits. Six bits alone would produce a single-digit answer, which reads badly next to real machine code.
Read the hex as byte pairs
The lower field spaces its digits in twos, so each pair you see is one byte of the encoding — the same grouping an object dump uses.
Take it with the 0x marker
Copying the hex field hands you 0x plus the digits and drops the display spaces, which is exactly the literal a C or Rust source file wants.
Bit Patterns Behind x86 Opcode Bytes
Single-byte x86 opcodes are a good way to sanity-check your bit reading, because the mnemonic is unambiguous and every debugger prints the same value. Encode the pattern, compare with the byte below, and you know your nibble split is right.
| Bit pattern | Hex byte | x86 instruction |
|---|---|---|
01010101 | 55 | PUSH the base pointer — the classic prologue byte |
10010000 | 90 | NOP, the byte used to pad and to blank out code |
11000011 | C3 | RET, the near return that ends most functions |
11001100 | CC | INT3, the software breakpoint a debugger writes in |
11101011 | EB | JMP with a one-byte signed displacement |
11110100 | F4 | HLT, which parks the processor until an interrupt |
Output Spaced as Bytes
Hex digits appear in pairs, so a multi-byte encoding lines up with the columns of a disassembly listing.
Upper-Case Digits Throughout
Letters always come back as A to F, matching the convention most assemblers and hardware manuals print.
Long Encodings Stay Exact
Bit strings past fifteen characters switch to big-integer arithmetic, so a 64-bit immediate keeps every digit intact.
Machine Code Questions
My field layout is 12 bits — where do the missing bits go?
Nibbles are counted from the right, so a 12-bit string produces three hex digits and nothing is lost. If the hardware stores that field inside a 16-bit word, type the four extra zeros on the left yourself to get the four-digit form.
Why did my leading zeros disappear from the answer?
The result is the plain value, so 00001111 comes back as F rather than 0F. Add the zero in front when you write the byte down; the number is identical either way.
Does this tell me the byte order in memory?
No. You get the numeric value of the pattern. Whether a multi-byte word is stored low byte first is a property of the processor, so a little-endian machine will show those bytes reversed in a memory dump.
Can I encode a negative displacement this way?
Only if you do the two's complement step first. Work out the bit pattern for the negative value at your chosen width, then convert that pattern — the minus sign itself is not accepted in the input.
Why is hex the standard for machine code rather than octal?
Modern hardware is organised in eight-bit bytes, and eight divides evenly into two nibbles. Octal digits cover three bits, which straddles byte boundaries — that suited the 12-bit and 36-bit machines of the 1960s, not today's.
No comments yet. Be the first to comment!