int main() { unsigned number; /* value from user */ unsigned pow; /* number of bits to left shift */ unsigned result; /* result of shift */ /* prompt user and read two integers */ printf( "Enter two integers: " ); scanf( "%u%u", &number, &pow ); /* display bits of number */ printf( "\nnumber:\n" ); displayBits( number ); /* display bits of pow */ printf( "\npow:\n" ); displayBits( pow ); /* perform shift and display results */ result = power2( number, pow ); printf( "\n%u * 2^%u = %u\n", number, pow, result ); displayBits( result ); printf("\n"); // output blank line for appearances system("pause"); // wait until user signals completion return 0; /* indicate successful termination */ } /* end main */
void unpackCharacters(unsigned s){ unsigned tmp; char c1; char c2; tmp=s&65280; tmp>>=8; c1=tmp; displayBits(c1); tmp=s&255; c2=tmp; displayBits(c2); }
int main(void) { unsigned int a = 100000; printf("Before: "); displayBits(a); printf("\nAfter: "); a >>= 4; displayBits(a); printf("\n"); return 0; }
int main (void) { unsigned ui; printf("\n%s%u%s","Please enter an unsigned integer " "(whole number between 0 and ", ULONG_MAX, "):\n\n"); scanf("%u", &ui); displayBits(ui); printf("\n\n%s%u%s", "The result of reversing ", ui, " is:\n"); displayBits(reverseBits(ui)); waitOnInput(); return 0; }
int hammingStringCopyWithParityResetCoreFunction(char *input_hamming_string, char *new_hamming_string, MODE mode) { int len,i; /**Calculating the incoming hamming message length*/ len = messageLength(input_hamming_string,mode); /**Loop which unsets all the parity bits in the hamming string.*/ for (i = 0; i < len; ++i) { /**Condition to check if the read bit position is a parity bit position or not.*/ if((i+1)==closestPowerOfTwo((i+1),mode)) { /**Set the bit position as empty as indicated by the character _ */ *(new_hamming_string+i) = '_'; } else { *(new_hamming_string+i) = *(input_hamming_string+i); } } *(new_hamming_string+i) = CHARACTER_TERMINATION; /**To check if the mode being executed was debug or not.*/ if(mode == eDEBUG) { /**function call to display the bitstring.*/ displayBits(new_hamming_string); } return 0; }
/** CORE FUNCTIONS */ int generateInitialHammingStringCoreFunction(char *input_string, char *hamming_string, MODE mode) { int i,j,input_string_len; j=0; /**Calculating the hamming message length*/ input_string_len = messageLength(input_string,mode); /**Loop to generate a hamming outline string with unset parity bits.*/ for (i = 0; i<input_string_len; ) { /**Condition check to verify if the bit position is a parity position or not.*/ if ((j+1)==closestPowerOfTwo((j+1), mode)) { /**Set the bit position as empty as indicated by the character _*/ *(hamming_string+j) = '_'; j++; } else { *(hamming_string+j) = *(input_string+i); j++; i++; } } *(hamming_string+j) = CHARACTER_TERMINATION; /**To check if the mode being executed was debug or not.*/ if(mode == eDEBUG) { /**function call to display the bitstring.*/ displayBits(hamming_string); } return 0; }
unsigned packCharacters(char c1,char c2){ unsigned pack; pack=c1; pack<<=8; pack|=c2; displayBits(pack); return pack; }
int main() { unsigned temp; // used to display the bits in a char unsigned packedBits; // to contain result of packing bits char input[MAX_INDEX]; // Array to contain the 4 input chars // complete main function code for(int i = 0; i < MAX_INDEX; i++) { printf("\nEnter a character: "); input[i] = getchar(); while(getchar() != '\n'); temp = input[i]; printf("\n"); displayBits(temp, MAX_BITS); } packedBits = packCharacters(input); printf("\n"); displayBits(packedBits, MAX_BITS); printf("\n"); return EXIT_SUCCESS; }
void displaySpongeIntermediateValuesOne(const unsigned char *message, unsigned int messageLength, unsigned int rate, unsigned int capacity) { Keccak_SpongeInstance sponge; unsigned char output[512]; unsigned char *messageInternal; messageInternal = malloc((messageLength+7)/8); alignLastByteOnLSB(message, messageInternal, messageLength); displayBytes(1, "Input message (last byte aligned on MSB)", message, (messageLength+7)/8); displayBits(2, "Input message (in bits)", message, messageLength, 1); displayBits(2, "Input message (in bits, after the formal bit reordering)", messageInternal, messageLength, 0); displayBytes(2, "Input message (last byte aligned on LSB)", messageInternal, (messageLength+7)/8); Keccak_SpongeInitialize(&sponge, rate, capacity); displayStateAsBytes(1, "Initial state", sponge.state); Keccak_SpongeAbsorb(&sponge, messageInternal, messageLength/8); if ((messageLength % 8) != 0) Keccak_SpongeAbsorbLastFewBits(&sponge, messageInternal[messageLength/8] | (1 << (messageLength % 8))); Keccak_SpongeSqueeze(&sponge, output, sizeof(output)); free(messageInternal); }
int parityCalculationInHammingStringCoreFunction(char *hamming_string, MODE mode) { int i,j,hamming_message_len,parity=0; /**Calculating the hamming message length*/ hamming_message_len = messageLength(hamming_string, mode); /** Loop which calculates the parity bits values from the provided message string.*/ for (i = 0; i < hamming_message_len; i++) { /** Check if the read bit position is a parity bit or not. Principle used behind the parity bits unset and set is the priciple used in Turing machines when played around with marking messages. */ if(*(hamming_string+i) == '_') { /**Setting it to zero before proceeding.*/ *(hamming_string+i) = '0'; /**Loop which calculates the parity of the given message for the corresponding parity bit.*/ for (j = i+1; j < hamming_message_len; ++j) { /**Check if the read bit position matches the parity bit position range.*/ if (((j+1)&(i+1)) != 0) { /**XORing the incoming bit position with the present parity value.*/ parity ^= (*(hamming_string+j) - ASCII_CONV_ZERO); } } /**To check if the mode being executed was debug or not.*/ if(mode == eDEBUG) { /**Prints the resultant parity value.*/ printf("Parity in Bit Position %d : %d\n", (i+1), parity); } /** Since the values are stored as characters. The resultant parity is converted to equivalent character and stored in the hamming string. */ *(hamming_string+i) = parity + ASCII_CONV_ZERO; /**temporary variable for calculatin parity is reset.*/ parity = 0; } } /**To check if the mode being executed was debug or not.*/ if(mode == eDEBUG) { /**function call to display the bitstring.*/ displayBits(hamming_string); } return 0; }
int main( void ) { unsigned int number1; unsigned int number2; unsigned int mask; unsigned int setBits; // demonstrate bitwise AND (&) number1 = 65535; mask = 1; puts( "The result of combining the following" ); displayBits( number1 ); displayBits( mask ); puts( "using the bitwise AND operator & is" ); displayBits( number1 & mask ); // demonstrate bitwise inclusive OR (|) number1 = 15; setBits = 241; puts( "\nThe result of combining the following" ); displayBits( number1 ); displayBits( setBits ); puts( "using the bitwise inclusive OR operator | is" ); displayBits( number1 | setBits ); // demonstrate bitwise exclusive OR (^) number1 = 139; number2 = 199; puts( "\nThe result of combining the following" ); displayBits( number1 ); displayBits( number2 ); puts( "using the bitwise exclusive OR operator ^ is" ); displayBits( number1 ^ number2 ); // demonstrate bitwise complement (~) number1 = 21845; puts( "\nThe one's complement of" ); displayBits( number1 ); puts( "is" ); displayBits( ~number1 ); } // end main