Skip to content

Instantly share code, notes, and snippets.

@JHeld07
Created March 26, 2015 02:53
Show Gist options
  • Save JHeld07/a00450742b5ef9727138 to your computer and use it in GitHub Desktop.
Save JHeld07/a00450742b5ef9727138 to your computer and use it in GitHub Desktop.
//design problem 1
#include <stdio.h>
#include <conio.h>
void binaryprint(int x);
main(){
int x=0;
printf("Print the Binary format of 1-20 \n");
while(x<=20){
binaryprint(x);
x++;
printf("\n");
}
while(!kbhit()){}
}
void binaryprint(int x){
int mask=0x80;
while(mask>=0x01){
if(mask==0x08){
printf(" ");
}
if(x&mask){
printf("1");
}
else{
printf("0");
}
mask=mask>>1;
}
}
*/
#include <stdio.h>
#include <conio.h>
void binaryprint(int x);
int binaryset(int numb, int loc);
int binaryreset(int numb, int loc);
int binarytoggle(int numb, int loc);
//int binaryread(int numb, int loc);
main(){
int op,numb=0,loc;
do{
printf("Enter a bit in the binary code you would like to operate\n");
scanf(" %d", &loc);
printf("Press 1 to set the bit you entered. \nPress 2 to reset the bit you entered. \nPress 3 to toggle the bit you entered.\n");
scanf(" %d", &op);
if(op==1){
numb=binaryset(numb, loc);
}
else if(op==2){
numb=binaryreset(numb, loc);
}
else if(op==3){
numb=binarytoggle(numb, loc);
}
else{
printf("Try again\n");
}
binaryprint(numb);
}while(5);
printf("i left");
while(!kbhit()){}
}
void binaryprint(int x){
int mask=0x80;
while(mask>=0x01){
if(mask==0x08){
printf(" ");
}
if(x&mask){
printf("1");
}
else{
printf("0");
}
mask=mask>>1;
}
}
int binaryset(int numb, int loc){
int mask = 0x01;
numb=numb|(mask<<loc);
return(numb);
}
int binaryreset(int numb, int loc){
int mask = 0x01;
numb=numb&~(mask<<loc);
return(numb);
}
int binarytoggle(int numb, int loc){
int mask = 0x01;
numb=numb&(mask<<loc);
return(numb);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment