module add1 ( input a, input b, input cin, output sum, output cout );
assign sum = (a + b + cin) & 1;
assign cout = ((a + b + cin) >> 1); // no "& 1" at the end
endmodule
and note that cout is fixed to zero, unless a & 1 is added to the end. Go read the answer, but the gist is that a + b + cin has type bit, and shifting bit by one to the right shifts it off the end.
What does that shift mean? Should we even have a shift operator also? Is >> N equivalent to [size:N], and << N equivalent to ++ 0'bN?
As a side note, depending on our behaviour, I think we have an opportunity here to throw a warning on (a[t] + b[t] + cin[t]) >> 1, since we know that's zero and that clearly isn't what the user wanted.
Look at https://electronics.stackexchange.com/questions/624400/why-does-adding-1-to-an-assign-statement-produce-a-completely-different-synt
Here's their code:
and note that
cout
is fixed to zero, unless a& 1
is added to the end. Go read the answer, but the gist is thata + b + cin
has typebit
, and shiftingbit
by one to the right shifts it off the end.What does that shift mean? Should we even have a shift operator also? Is
>> N
equivalent to[size:N]
, and<< N
equivalent to++ 0'bN
?As a side note, depending on our behaviour, I think we have an opportunity here to throw a warning on
(a[t] + b[t] + cin[t]) >> 1
, since we know that's zero and that clearly isn't what the user wanted.