The compiler warns against mixing ints and bytes in bitwise operations. The best way I could find to create byte literals was the constructor syntax byte(1), as shown in the for loops of the example code below. Is this the best way? (Besides defining my own literal suffix)
#include<vector>
using namespace std;
void drawMonochromeHLine(vector<byte>& screen, int width, int x1, int x2, int y) {
if (x2 < x1) swap(x1, x2);
//DCHECK(x1 >= 0);
//DCHECK((x2 >> 3) < width)
//DCHECK(y < height)
int yOffset = y * (width >> 3);
int si = (x1 >> 3) + yOffset;
int ei = (x2 >> 3) + yOffset;
int sbi = (~x1) & 0b111; // x1=6: 00000011. x1=1: 01111111
int ebi = (~x2) & 0b111; // x=7: 10000000. x2=1: 01111111
if (si == ei) {
for (byte mask = byte(1) << ebi; mask >= byte(1) << sbi; mask >>= 1)
screen[si] |= mask;
}
else {
for (byte mask = byte(1) << sbi; mask > byte(0); mask >>= 1)
screen[si] |= mask;
for (int i = si + 1; i < ei; ++i)
screen[i] = byte(0xff);
for (byte mask = byte(1) << 7; mask >= byte(1) << ebi; mask >>= 1)
screen[ei] |= mask;
}
}