Skip to content

Commit

Permalink
completed day 162
Browse files Browse the repository at this point in the history
  • Loading branch information
nkane committed Mar 30, 2018
1 parent 53fa8d2 commit 72ed574
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
6 changes: 6 additions & 0 deletions day-162/problem-1/build.bat
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
53 changes: 53 additions & 0 deletions day-162/problem-1/main.c
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;
}

0 comments on commit 72ed574

Please sign in to comment.