How would I write something that fills with the right-most bit (<<< is used to denote this non-existent operator):1 <<< 7: 11111111 and 0 <<< 7: 00000000 9 <<< 1: 10011 and 10 <<< 7: 10100000000 as in the reverse of the Sign-propagating right shift (which fills with the left-most digit):-256 >> 8: 11111111111111111111111100000000 >> 11111111111111111111111111111111 and 16777215 >> 8: 00000000111111111111111111111111 >> 00000000000000001111111111111111?
LSB-propagating left shift, conceptual equivalent of sign-propagating right shift
bitwise operators
Well if I didn't care about performance (and I never do until I have to) I'd do it the simplest way I can think of.Presuming a language (such as c) where I have the arithmetic right shift (what you call a sign-propagating right shift), and presuming I know how to reverse the bits in a register then I can build your left shift equivalent like so:reverse bitsarithmetic right shift x bits reverse bits 1 <<< 7: 11111111 00000001 <- 110000000 <- reverse   11111111 <- arithmetic right shift (7) 11111111 <- reverse0 <<< 7: 0000000000000000 <- 000000000 <- reverse   00000000 <- arithmetic right shift (7)00000000 <- reverse   15 <<< 3: 0111111100001111 <- 1511110000 <- reverse   11111110 <- arithmetic right shift (3)01111111 <- reverse   This is likely slow but uses well tested code and is conceptually simple. So at the least it can be the bedrock for tests against any faster methods.This next idea is not tested but off the top of my head a more direct route would be to left shift, mask out the bit that you need propagate, subtract 1, and or with the result of the shift.    15 <<< 3 :01111111    00001111 <- 15    01111000 <- left shift     00001000 <- mask out propagating bit.  If result is zero, return the shift    00000111 <- subtract 1    01111111 <- OR with result of shift (01111000)I wouldn't trust this until I'd tested it.  But this seems to work:       result = ((n&1)==1)?(n<<x|(1<<x)-1):(n<<x);Done without branching, a more performant version, suggested by @ErikEidt, would be:    y = (n & 1);     result = ((n << x) | ((y << x) - y));Done this way the shift (n<<x) is OR'ed with zero's when not propagating so there is no need to branch. 