forked from nkane/c-practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
IF NOT EXIST .\build MKDIR .\build | ||
PUSHD .\build | ||
|
||
cl /Od /MTd /Zi /nologo ..\main.c | ||
|
||
POPD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Write a C program that displays the first 8 bits of each character value input into a variable named | ||
* ch. (Hint: assuming each character is stored using 8 bits, start by using the hexadecimal mask 80, | ||
* which corresponds to the binary number 10000000. If the result of the masking operation is 0; else | ||
* display a 1. Then shift the mask one place to the right and examine the next bit, and so on until | ||
* all bits in the variable ch have been processed | ||
* | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <stdint.h> | ||
|
||
int | ||
main() | ||
{ | ||
uint8_t charMask; | ||
char ch = ' '; | ||
|
||
printf("Keep entering in characters to get their binary value (or ! to quit):\n"); | ||
|
||
while (1) | ||
{ | ||
charMask = 0x80; | ||
ch = getchar(); | ||
getchar(); | ||
|
||
if (ch == '!') | ||
{ | ||
break; | ||
} | ||
|
||
uint8_t bits = 8; | ||
uint8_t v; | ||
printf("%c - bits: ", ch); | ||
while (bits > 0) | ||
{ | ||
v = (charMask & ch); | ||
charMask = (charMask >> 1); | ||
if (v > 0) | ||
{ | ||
printf("1"); | ||
} | ||
else | ||
{ | ||
printf("0"); | ||
} | ||
bits--; | ||
} | ||
printf("\n"); | ||
} | ||
|
||
return 0; | ||
} |