def rrx(carry, value, count, width=32):
    """
    Rotate Right Extended
    (original carry must also be passed in)

    >>> rrx(0, 0xff, 4) == (1, 0xe000000f)
    True
    >>> rrx(1, 0xff, 4) == (1, 0xf000000f)
    True
    """
    count %= width
    value &= (1 << width) - 1
    # First shift 1 to the left to leave room for the carry.
    value <<= 1
    # Stick in the original carry.
    value |= carry & 1
    value = (value >> count) | (value << ((width + 1) - count))
    carry = value & 1
    value >>= 1
    value &= (1 << width) - 1
    return carry, value