Bird
Raised Fist0
Verilogprogramming~20 mins

Subtractor design in Verilog - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Subtractor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of 4-bit subtractor module
What is the output of the following 4-bit subtractor module when inputs A = 4'b1010 and B = 4'b0011?
Verilog
module subtractor_4bit(input [3:0] A, input [3:0] B, output [3:0] Diff, output Borrow);
  assign {Borrow, Diff} = A - B;
endmodule
ADiff = 4'b1011, Borrow = 0
BDiff = 4'b1011, Borrow = 1
CDiff = 4'b0111, Borrow = 1
DDiff = 4'b0111, Borrow = 0
Attempts:
2 left
💡 Hint
Remember that Borrow is the carry out bit from subtraction in Verilog when using the assign with concatenation.
🧠 Conceptual
intermediate
1:30remaining
Understanding borrow in binary subtraction
In a 1-bit subtractor, what does the borrow output represent?
AIt is the sum of the two input bits.
BIt shows if the subtraction result is zero.
CIt indicates if the minuend bit is smaller than the subtrahend bit, requiring borrowing from the next higher bit.
DIt indicates overflow in subtraction.
Attempts:
2 left
💡 Hint
Think about what happens when you subtract a larger bit from a smaller bit in binary.
🔧 Debug
advanced
2:00remaining
Identify the error in this subtractor module
What error will occur when compiling this Verilog subtractor module?
Verilog
module subtractor(input a, input b, output diff, output borrow);
  assign diff = a ^ b;
  assign borrow = ~a & b;
endmodule
ANo error, module compiles and works correctly
BSyntaxError due to missing semicolon after borrow assignment
CRuntime error due to wrong borrow logic
DSyntaxError due to missing module end keyword
Attempts:
2 left
💡 Hint
Check each line carefully for missing punctuation.
📝 Syntax
advanced
2:30remaining
Correct syntax for 1-bit full subtractor
Which option correctly implements a 1-bit full subtractor in Verilog?
A
module full_subtractor(input a, input b, input bin, output diff, output bout);
  assign diff = a ^ b ^ bin;
  assign bout = (~a & b) | ((~a | b) & bin);
endmodule
B
module full_subtractor(input a, input b, input bin, output diff, output bout);
  assign diff = a ^ b ^ bin;
  assign bout = (~a & b) | ((~a & b) & bin);
endmodule
C
module full_subtractor(input a, input b, input bin, output diff, output bout);
  assign diff = a ^ b ^ bin;
  assign bout = (~a & b) & ((~a | b) | bin);
endmodule
D
module full_subtractor(input a, input b, input bin, output diff, output bout);
  assign diff = a ^ b ^ bin;
  assign bout = (~a | b) & ((~a & b) | bin);
endmodule
Attempts:
2 left
💡 Hint
Recall the borrow output formula for a full subtractor: bout = (~a & b) | ((~a | b) & bin)
🚀 Application
expert
2:00remaining
Number of bits in output of subtractor with borrow
If you design an 8-bit subtractor module in Verilog that outputs the difference and borrow, how many bits should the borrow output have and why?
A1 bit, because borrow indicates if the entire subtraction required borrowing from a higher bit
B8 bits, one borrow bit per input bit
C9 bits, to hold borrow and difference bits combined
D0 bits, borrow is not needed in multi-bit subtractors
Attempts:
2 left
💡 Hint
Think about how borrow is represented in multi-bit subtraction.

Practice

(1/5)
1.

What is the main function of a subtractor in digital circuits?

easy
A. To multiply two binary numbers
B. To add two binary numbers
C. To find the difference between two binary numbers
D. To store binary data

Solution

  1. Step 1: Understand the subtractor's purpose

    A subtractor is designed to calculate the difference between two binary inputs.
  2. Step 2: Compare with other operations

    Adders add, multipliers multiply, and storage units store data, so these are not subtractors.
  3. Final Answer:

    To find the difference between two binary numbers -> Option C
  4. Quick Check:

    Subtractor = Difference finder [OK]
Hint: Subtractor always calculates difference, not sum or product [OK]
Common Mistakes:
  • Confusing subtractor with adder
  • Thinking subtractor stores data
  • Mixing subtractor with multiplier
2.

Which of the following is the correct Verilog module header for a 1-bit subtractor?

module subtractor(
easy
A. module subtractor(input a, input b, output diff, output borrow);
B. module subtractor(input a, b, output diff, borrow);
C. module subtractor(a, b, diff, borrow);
D. module subtractor(input a, input b, input diff, output borrow);

Solution

  1. Step 1: Check correct input/output declaration

    Inputs must be declared with 'input' keyword and outputs with 'output'.
  2. Step 2: Verify syntax correctness

    module subtractor(input a, input b, output diff, output borrow); correctly declares inputs and outputs with proper keywords and commas.
  3. Final Answer:

    module subtractor(input a, input b, output diff, output borrow); -> Option A
  4. Quick Check:

    Correct input/output syntax = module subtractor(input a, input b, output diff, output borrow); [OK]
Hint: Always declare inputs and outputs explicitly with keywords [OK]
Common Mistakes:
  • Omitting input/output keywords
  • Listing inputs and outputs without keywords
  • Declaring outputs as inputs
3.

What is the output difference and borrow when inputs a=1 and b=0 are applied to this 1-bit subtractor module?

assign diff = a ^ b ^ borrow_in;
assign borrow_out = (~a & b) | (borrow_in & (~a ^ b));

Assume borrow_in = 0.

medium
A. diff = 0, borrow_out = 0
B. diff = 0, borrow_out = 1
C. diff = 1, borrow_out = 1
D. diff = 1, borrow_out = 0

Solution

  1. Step 1: Calculate diff with given inputs

    diff = a ^ b ^ borrow_in = 1 ^ 0 ^ 0 = 1
  2. Step 2: Calculate borrow_out with given inputs

    borrow_out = (~a & b) | (borrow_in & (~a ^ b)) = (0 & 0) | (0 & (0 ^ 0)) = 0
  3. Final Answer:

    diff = 1, borrow_out = 0 -> Option D
  4. Quick Check:

    1 - 0 = 1, no borrow needed [OK]
Hint: XOR inputs and borrow_in for diff; borrow if a < b [OK]
Common Mistakes:
  • Forgetting borrow_in in diff calculation
  • Misapplying borrow logic
  • Mixing up a and b in borrow expression
4.

Identify the error in this Verilog code for a 1-bit subtractor:

module subtractor(input a, input b, output diff, output borrow);
  assign diff = a - b;
  assign borrow = (b > a);
endmodule
medium
A. Using '-' operator for bitwise subtraction is incorrect in Verilog
B. 'borrow' should be declared as 'output reg' instead of 'output'
C. The module lacks a clock input for synchronous operation
D. The borrow calculation logic is incorrect

Solution

  1. Step 1: Check arithmetic operator usage

    Verilog '-' operator performs arithmetic subtraction but does not produce borrow output directly.
  2. Step 2: Understand borrow signal generation

    Borrow must be calculated using logic expressions, not by comparing b and a with '>' operator.
  3. Final Answer:

    Using '-' operator for bitwise subtraction is incorrect in Verilog -> Option A
  4. Quick Check:

    Subtractor needs logic for borrow, not '-' operator [OK]
Hint: Use logic expressions for borrow, not '-' operator [OK]
Common Mistakes:
  • Using arithmetic '-' expecting borrow output
  • Ignoring borrow logic
  • Assuming '>' operator works for borrow
5.

Design a 2-bit subtractor using two 1-bit subtractor modules connected in series. Which of the following correctly describes the borrow connection?

hard
A. The borrow input of the least significant bit subtractor connects to the borrow output of the most significant bit subtractor
B. The borrow output of the least significant bit subtractor connects to the borrow input of the most significant bit subtractor
C. The borrow outputs of both subtractors are connected together to form a single borrow output
D. The borrow inputs of both subtractors are tied to 0

Solution

  1. Step 1: Understand borrow chaining in multi-bit subtractors

    Borrow from the lower bit subtractor must feed into the higher bit subtractor's borrow input.
  2. Step 2: Verify connection direction

    The least significant bit's borrow output connects to the most significant bit's borrow input to propagate borrow correctly.
  3. Final Answer:

    The borrow output of the least significant bit subtractor connects to the borrow input of the most significant bit subtractor -> Option B
  4. Quick Check:

    Borrow chains from LSB to MSB [OK]
Hint: Borrow flows from lower to higher bits in subtractors [OK]
Common Mistakes:
  • Connecting borrow inputs to zero for all bits
  • Connecting borrow outputs together incorrectly
  • Reversing borrow input/output connections
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard