Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution for 5_8 #136

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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;
}