Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion c++/Chapter 5/Question5_8.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,53 @@
#include<iostream>
using namespace std;

void PrintScreen(unsigned char buffer[], int width, int height){
int byteWidth = width / 8;
for(int row = 0; row < height; ++row){
for(int column = 0; column < byteWidth; ++column){
int index = row * byteWidth + column;
char block = buffer[index];
for(int i = 7; i >= 0; --i){
if( (block >> i) & 1){
cout << '.';
}
else{
cout << ' ';
}
}
}
cout << '\n';
}
}

void SetBit(unsigned char& byte, int bit){
byte |= 1 << bit;
}

int main(){
void DrawHorizontalLine(unsigned char buffer[], int bufferLength, int width, int x1, int x2, int y){
int x = width * y + x1;
int end = width * y + x2;
while(x < end){
int byteIndex = x / 8;
int bit = x - byteIndex * 8;
SetBit(buffer[byteIndex], 7 - bit);
++x;
}
}

int main(){
unsigned char screen[] = {
0x80, 0x01,
0x40, 0x02,
0x20, 0x04,
0x10, 0x08,
0x08, 0x10,
0x04, 0x20,
0x02, 0x40,
0x01, 0x80
};
PrintScreen(screen, 16, 8);
DrawHorizontalLine(screen, 2 * 8, 16, 5, 9, 2);
PrintScreen(screen, 16, 8);
return 0;
}