signExtend method

Logic signExtend(
  1. int newWidth
)

Returns a new Logic width width newWidth where new bits added are sign bits as the most significant bits. The sign is determined using two's complement, so it takes the most significant bit of the original signal and extends with that.

The newWidth must be greater than or equal to the current width or an exception will be thrown.

Implementation

Logic signExtend(int newWidth) {
  if (width == 1) {
    return ReplicationOp(this, newWidth).replicated;
  } else if (newWidth > width) {
    return [
      ReplicationOp(this[width - 1], newWidth - width).replicated,
      this,
    ].swizzle();
  } else if (newWidth == width) {
    // ignore: avoid_returning_this
    return this;
  }

  throw Exception(
      'New width $newWidth must be greater than or equal to width $width.');
}