0

I have an 8-bit input A and also 3-bit input n. I want to shift A n times to left or right but this code doesn't seem to work (the output is x):

w = A << n;

But when i put an integer like 2 instead of n, the code works without problem. So how to convert n to an integer value so the shift operation can work without problem?

Ju Bc
  • 173
  • 2
  • 11

1 Answers1

0

The shift operators always treat the expression on the RHS as unsigned. You also need to declare n as a signed variable, or cast to signed

bit signed [2:0] n;
assign w = (n >= 0) ? A << n : A>> -n;
bit [2:0] n;
assign w = (signed'(n) >= 0) ? A << n : A >> -n;
dave_59
  • 39,096
  • 3
  • 24
  • 63