In this homework you are asked to implement three functions in C. Details of eac
ID: 3844442 • Letter: I
Question
In this homework you are asked to implement three functions in C. Details of each function is given in the skeleton code provided. Attached you are given three files. The files are named as :
BitManipulations.h : This header file provides common variables and type definitions.
BitFunctions.c : This file contains the skeleton code for three C functions. This file is given only for information purposes. You will implement all three functions in C.
BitManipulations.c : This file contains the main function that will be used to test the functions you will write. In the file BitFunctions.c , three functions are defined but not implemented.
int countNumberofOnes(uint32_t *intData)
void setBit(uint32_t *inData, uint32_t bitPosition,uint32_t value)
int hammingDistance(uint32_t inData1, uint32_t inData2)
Please complete the implementation of the four functions IN C. Test your functions using the main program given. Rename the file BitFunctions.c as BitFunctions_yourredID.c and return only this file.
This is bit manipulation.h file:
Explanation / Answer
/*
* BitFunctions.c
*
* Created on: Feb 10, 2015
* Author: Mr. Knick
*/
/*
* BitFunctions.c
*
* Created on: 2/11/2015
* Author: Brent Knickerbocker
*/
//#include "BitManipulations1.h"
//************************************************************************************/
//
// invertBits
//
// Description:Accepts a pointer of size uint32_t and inverts
// Each bit of the input data passed
// Preconditions:input argument is passed as a pointer
// Postconditions:The inverted value is returned in the pointer
//
// Calls: N/A
// Called by: main
//
//***********************************************************************************/
void invertBits(uint32_t *intData)
{
*intData=~*intData;//tried using xor but it gave funky results this gave propper negation
}
//***********************************************************************************/
//
// writeinBinary
//
// Description: Prints the argument passed in Binary -for example if 15 is passed
// it will print 00000000000000000000000000001111
// Preconditions:
// Postconditions:
//
// Calls: N/A
// Called by: main
//
//***********************************************************************************/
void writeinBinary(uint32_t inData)
{
printf("Value of bits: ");
unsigned int n = inData;
int unsigned arrayofints[32]={0};//create array for temp storage
int i=31;
while (n) { // this code was modified from url http://stackoverflow.com/questions/6373093/how-to-print-binary-number-via-printf
if (n & 1)
{
arrayofints[i]= 1;
}
n >>= 1;
i--;
}
for(i=0;i<32;i++)
{
printf("%d",arrayofints[i]);//use array to print to ensure propper padding
}
printf(" ");
}
//***********************************************************************************/
//*
//* setBit
//*
//* Description: The function sets the bit in the specified bit position in an to the specifid value.
//* Preconditions: Value can be a 1 or 0. bitPosition will be between 0 and 31 (for integer size argument)
//* Postconditions: The bit of *inData at position biPosition will be set to value
//*
//* Calls: N/A
//* Called by: main
//*
//***********************************************************************************/
void setBit(uint32_t *inData, uint32_t bitPosition,uint32_t value)
{
int unsigned valuetoxor=0;
valuetoxor=value<<bitPosition;//find that place in the value and only change that
*inData=*inData^valuetoxor;//xor with 0 except in the place we want to change
}
//***********************************************************************************/
//* hammingDistance
//* Description: Function hammingDistance calculates total number of bits
//* that need to be inverted in order to change inData1 into inData2 or vice versa.
//* Preconditions: The function accepts two unsigned integers as input
//* Postconditions: The function returns the hamming distance
//*
// Calls: N/A
// Called by: main
//*
//***********************************************************************************/
int hammingDistance(uint32_t inData1, uint32_t inData2)
{
//this code is modified from this url http://e...content-available-to-author-only...a.org/wiki/Hamming_distance
int dist;
unsigned val;
dist = 0;
val = inData1 ^ inData2; // leaves only what is different
// Count the number of bits set
while (val != 0)
{
// A bit is set, so increment the count and clear the bit
dist++;
val &= val - 1;
}
// returns the number of different bits
return dist;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.