Codiode

Absolute Value with Bit Growth

MediumVerilog / SystemVerilogBuild

The absolute value of -128 is +128, which doesn't fit in 8 signed bits. For an 8-bit signed integer, the representable range is -128 to +127, so the result needs a 9th bit.

The module computes the absolute value of an 8-bit signed two's complement input and outputs the result as a 9-bit unsigned integer. This bit growth ensures that the most negative 8-bit value is correctly represented without wrapping or data loss.

This is a purely combinational circuit. There is no clock or reset. The output must reflect the absolute value of the input continuously.

Example evaluations:

  • Input 8'b00000000 (0) produces output 9'b000000000 (0)
  • Input 8'b01111111 (127) produces output 9'b00001111111 (127)
  • Input 8'b11111111 (-1) produces output 9'b000000001 (1)
  • Input 8'b10000000 (-128) produces output 9'b010000000 (128)
Signal Direction Width Description
a input 8 Signed two's complement input value
abs_a output 9 Unsigned absolute value of the input

Constraints

  • Combinational logic only; no sequential elements or latches.
  • The input a is strictly an 8-bit signed two's complement integer.
  • The output abs_a is strictly a 9-bit unsigned integer.

Topics

ArithmeticCombinational LogicData Types

Solve this problem

Write the module in Verilog, SystemVerilog or VHDL. Your submission is compiled and simulated against the problem's testbench. You get the waveform back, not a stored answer.

This problem is part of Codiode Pro. The statement above is free to read.

The circuit builder and code editor need a desktop screen. On a phone, read the problem here and open it on a laptop to solve.

Part of Datapath Design

Problem 10 of 27 in the Datapath Design track.

Related problems

Browse all problems · Verilog practice problems · Learning tracks

Solve this on a laptopSend yourself the link and continue there.