#include int power(int base, int exp){ //Feed me a base and a power and 'll return the result if (exp == 0) return(1); else return(base * (power(base, exp - 1))); } int main() { while(1){ char val[14]; //The string I read unsigned long int value; //The value i read from said stringval int i; //Loop Counter int j; //Nested Loop Counter int k = 0; //Free counter int newval[8]; //The 8 values of which I will set the final four bits for (i = 0; i < 8; ++i) //Initialize array newval[i] = 0; //GET DATA// printf("Gimme a value and I'll devide it into 8 4-bit pieces.\n"); fgets(val,sizeof(val),stdin); sscanf(val,"%d",&value); //PRINT IT AS 32 CONSECUTIVE BITS// printf("The value in binary looks like\n",value); for (i = 0; i < 32; ++i){ //increment while we arent at the last bit if ((value & (0x80000000 >> i)) != 0) //test against known 32 bit value of 1 followed by 31 0's shifted as needed printf("1"); //print 1 if there is a bit there else printf("0"); //else print 0 } printf("\n"); printf("\n"); printf("\n"); printf("Now I'll divide it into 8 groups of 4-bits.\n"); for (i = 0; i < 32; ++i){ //increment to last bit for (j = 0; j < 4; ++j){ //increment four bits if ((value & (0x80000000 >> (i + j))) != 0){ //test again printf("1"); //print newval[k] += power(2,4 - (j + 1)); //set array based on bit we found } else printf("0"); } printf(" = %d\n",newval[k]); //print what those 4 bits = and new line for next four i += (j - 1); //add the bits we counted in j already to i ++k; //check the next value in the array after this loop ends } printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("\n"); printf("\n"); } return (0); }