Why Does RISC-V Scramble the Immediate Bits in Branches and Jumps?

To keep the hardware simple. In RISC-V the register fields never move and the sign bit is always bit 31, so the immediate has to fit around them. The B and J layouts are chosen so that each bit of the immediate comes from the same instruction bit in as many formats as possible. That way the circuit that builds immediates needs only a few small multiplexers, even though the layouts look scrambled to a person reading them.

What you'll learn

  • Where each immediate bit sits in the I, S, B, U and J formats
  • Why branch and jump offsets don't store bit 0
  • How B is S with one bit moved, and J is a mix of U and I
  • How to decode a branch offset by hand

Conventions and assumptions

RV32I instruction formats from the RISC-V Unprivileged ISA specification, version 2.1. inst[n] is bit n of the 32-bit instruction; imm[n] is bit n of the immediate after it is reassembled.

Where each immediate bit comes from

Immediate bitsISBUJ
imm[0]inst[20]inst[7]always 00always 0
imm[4:1]inst[24:21]inst[11:8]inst[11:8]0inst[24:21]
imm[10:5]inst[30:25]inst[30:25]inst[30:25]0inst[30:25]
imm[11]inst[31]inst[31]inst[7]0inst[20]
imm[19:12]signsignsigninst[19:12]inst[19:12]
sign bitinst[31]inst[31]inst[31]inst[31]inst[31]

Read down any row and you see few distinct sources. imm[10:5] comes from inst[30:25] in every format that has it. imm[4:1] comes from one of two places. The sign is always inst[31]. A hardware immediate generator therefore needs a small multiplexer per bit, not a full crossbar.

Why bit 0 is dropped

Branch and jump targets are always at least 2-byte aligned (the compressed extension allows 16-bit instructions), so bit 0 of the offset is always 0 and storing it would waste a bit. B-type uses the freed bit to reach twice as far: offsets from −4096 to +4094 bytes with a 12-bit field, and J-type reaches ±1 MiB.

B is S with one bit moved

Compare S and B. Both keep imm[10:5] in inst[30:25] and imm[4:1] in inst[11:8]. S stores imm[0] in inst[7]; B has no imm[0], so it puts imm[11] there instead and moves imm[12] into the sign position, inst[31]. Likewise J keeps imm[19:12] where U has them and imm[10:1] where I has them, with imm[11] in I's imm[0] position, inst[20].

Worked example: decoding 0xfe208ce3

  • In binary: 1111111 00010 00001 000 11001 1100011.
  • opcode 1100011 is a branch; funct3 000 is beq; rs1 = 00001 (x1), rs2 = 00010 (x2).
  • inst[31] = 1 gives imm[12] = 1, the sign. inst[7] = 1 gives imm[11] = 1. inst[30:25] = 111111 gives imm[10:5]. inst[11:8] = 1100 gives imm[4:1]. imm[0] = 0.
  • Assembled: imm[12:0] = 1 1 111111 1100 0, which is 1111111111000 in 13 bits, or −8.
  • So the word is beq x1, x2, −8: branch back two instructions if x1 equals x2. GNU as encodes beq x1, x2, −8 as exactly this word.

Common mistakes

  • Reading the B-type bits in instruction order and getting a nonsense offset.
  • Forgetting to append the implied 0 at bit 0, which halves the offset.
  • Treating the offset as a count of instructions instead of bytes.
  • Assuming imm[11] of a branch is in inst[31]. In B-type it is inst[7].

Check your understanding

  • Q1. Which instruction bit holds imm[11] in a jal? Answer: inst[20].
  • Q2. What is the largest forward branch offset? Answer: 4094 bytes.
  • Q3. Why is the sign always in bit 31? Answer: so sign extension can start before the format is decoded.

Related

Sources

Last verified

Bit positions checked against the RV32I chapter of the RISC-V Unprivileged ISA specification, and the worked example against GNU as, on 14 November 2026.