Electrical – Bit size changing using Verilog


I want to write a 32 bit reg to 22 bit reg while conserving the number and its sign.

reg [31:0] test_dat0; //test_dat0 <= -4 (binaire)
reg [22:0] test_dat1;
test_dat1 <= test_dat0 ; ??? //what is the value of test_dat1 here

is it right ?
thank you

Best Answer

If you know the number is between -2097152 and 2097151, then just truncate the 10 MSBs (test_dat1 <= test_dat0[21:0]).

Otherwise you cannot fit a 32bit number into a 22bit register whilst maintaining the value. It would be equivalent to trying to fit 2-litres of soda into a 1-litre bottle without losing any.