Question

Please complete the implementation of the four functions IN C. //************************************************************************************/ // // countNumberofOnes // //...

Please complete the implementation of the four functions IN C.

//************************************************************************************/
//
//                                                          countNumberofOnes
//
//                 Description: Counts the number of 1s in an integer passed as argument
//                 Preconditions:input argument is passed as a pointer
//                 Postconditions:the number of 1s  returned 
//
//                 Calls:                       N/A
//                 Called by:           main
//
//***********************************************************************************/

int  countNumberofOnes(uint32_t *intData)
{
        // Write a function that  counts number of 1s in an integer passed 
        






}


//***********************************************************************************/
//*
//*                                                         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)
{
// Please do not treat the  integer as arrays , this question is about bit manipulations
// You will need to use bitwise operations to solve this question 




}

//***********************************************************************************/
//*                                                     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, unint32_t inData2)
{ 





}

Main function with the variables, and to test the functions.

#include <stdio.h>
#include "BitManipulations.h"



int main(int *argc, char **argv)
{
        uint32_t Number;
        uint32_t bitPosition;
        uint32_t value;
        int  numOnes; 
        
        uint32_t input1;
        uint32_t input2;
        int32_t hDist;


        value = 1;
        bitPosition = 21;
        Number = 15345;

        numOnes = countNumberofOnes(&Number);        

        setBit(&Number, bitPosition,value); // set bit in bitposition to value

        hDist = hammingDistance(input1, input2);        // Calculates hamming distance 
                
        printf("\nHamming Distance = %d \t number of Ones : %d\n", hDist, numOnes);

        return 0;
}
/*
 * BitManipulations.h
 */

#ifndef BITMANIPULATIONS_H_
#define BITMANIPULATIONS_H_


typedef  unsigned int   uint32_t;
typedef  unsigned short uint16_t;



#endif /* BITMANIPULATIONS_H_ */
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include <stdio.h>
#include <stdint.h>

//************************************************************************************/
//
// countNumberofOnes
//
// Description: Counts the number of 1s in an integer passed as argument
// Preconditions:input argument is passed as a pointer
// Postconditions:the number of 1s returned
//
// Calls: N/A
// Called by: main
//
//***********************************************************************************/

int countNumberofOnes(uint32_t *intData)
{
   uint32_t number = *intData;
   // Write a function that counts number of 1s in an integer passed
   int noofOnes = 0;
   while (number)
   {
       int digit = number % 10;
       if (digit == 1)
           noofOnes++;
       number = number / 10;
   }
   return noofOnes;
}


//***********************************************************************************/
//*
//* 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)
{
   // Please do not treat the integer as arrays , this question is about bit manipulations
   // You will need to use bitwise operations to solve this question
   //Storing the 32 bit Value
   uint32_t number = *inData;
   char bitArr[4] = {0};
   int index = 0;
   while (number)
   {
       bitArr[(index/8)] |= (number % 2) << (index % 8);
       number = number / 2;
       ++index;
   }

   bitArr[bitPosition / 8] |= value << ((bitPosition - 1) % 8);

}

//***********************************************************************************/
//* 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)
{
   //Bit array for inData1
   char bitArr1[4] = { 0 };

   int index = 0;
   while (inData1)
   {
       bitArr1[(index / 8)] |= (inData1 % 2) << (index % 8);
       inData1 = inData1 / 2;
       ++index;
   }
   //Bit array for inData2
   char bitArr2[4] = { 0 };
   int index1 = 0;
   while (inData2)
   {
       bitArr2[(index1 / 8)] |= (inData2 % 2) << (index1 % 8);
       inData2 = inData2 / 2;
       ++index1;
   }

   int hammingDistance = 0;
   for (uint32_t i = 0; i < 32; i++)
   {
       char byte1 = (bitArr1[i / 8] & (1 << (i % 8)));
       char byte2 = (bitArr2[i / 8] & (1 << (i % 8)));
       if (byte1 != byte2)
       {
           ++hammingDistance;
       }
   }
   return hammingDistance;

}
//Main function with the variables, and to test the functions.
//
//#include <stdio.h>
//#include "BitManipulations.h"

int main(int *argc, char **argv)
{
   uint32_t Number;
   uint32_t bitPosition;
   uint32_t value;
   int numOnes;

   uint32_t input1 = 28;
   uint32_t input2 = 24;
   int32_t hDist;


   value = 1;
   bitPosition = 21;
   Number = 15345;

   numOnes = countNumberofOnes(&Number);

   setBit(&Number, bitPosition, value); // set bit in bitposition to value

   hDist = hammingDistance(input1, input2); // Calculates hamming distance

   printf("\nHamming Distance = %d \t number of Ones : %d\n", hDist, numOnes);

   return 0;
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Please complete the implementation of the four functions IN C. //************************************************************************************/ // // countNumberofOnes // //...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • USE C Programming LANGUAGE ONLY PLEASE. We use <stdio.h> if that helps. For the following problems,...

    USE C Programming LANGUAGE ONLY PLEASE. We use <stdio.h> if that helps. For the following problems, create the prototype function, calling function “int main()”, and function. So the function prototype, function call, and function header/body should be seen in the answer. The processor directive is not needed. Show a sample call from the int main() function for all problems. If you can for the first one please show proof that it has no errors. 1. Write a function called DisplayColumbiaUniversity...

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • I need help with my homework please. I should write this program in C++ language and...

    I need help with my homework please. I should write this program in C++ language and use Inventory.h file (header file), Inventory.cpp file (implementation file), and main.cpp file (main program). This is the program: Demonstrate the class in a driver program. Input validation, don’t accept negative values for item number, quantity, or cost. 6. Inventory Class Design an Inventory class that can hold information and calculate data for items ma retail store's inventory. The class should have the following private...

  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

  • How would you answer this in visual studios c++ and text book were using is Data...

    How would you answer this in visual studios c++ and text book were using is Data Structures Using C++ Author: D.S. Malik. Please help. It has to be written in .cpp 1) Sum of numbers Write a recursive function that accepts an integer argument and returns the sum of the number as an argument. For example, i 50 is passed as an argument, the function will return the sum of 1,2, 3, 4, 50. 2) Counting of times a character...

  • C Part 1 Given a structure that store the highest temperature (HighTemp) and the lowest temperature (LowTemp) of a day:...

    C Part 1 Given a structure that store the highest temperature (HighTemp) and the lowest temperature (LowTemp) of a day: typedef struct {       char DayOfMonth[31];     /*  e.g.  “May 13”,  “July 9”  */       int HighTemp;         /*  the highest temperature of that day  */       int LowTemp;         /* the lowest temperature of that day */ } DayTemp; DayTemp  Temperature[365]; Temperature is an array of structure DayTemp.  Write a function that will return the average of HighTemp for a given number of days (NumberDay)...

  • In this assignment, you must complete the 5 functons below. Each function's body must consist of...

    In this assignment, you must complete the 5 functons below. Each function's body must consist of just a single C statement. Furthermore, the statement must not be a control statement, such as if, switch, or any kind of loop. For each problem, you are also restricted to the operators and constants as specified. */ /* 1. Write a function which zeros a variable x. A pointer to x is passed as a parameter. YOU MAY USE NO CONSTANTS in this...

  • Needs to be done in C, The function shown can be called to reverse the char....

    Needs to be done in C, The function shown can be called to reverse the char. 7.5 Bit Encryption 7.5.1 Problem Given a single character, apply a simple bitwise encryption algorithm and return the cipher character. 7.5.2 Preconditions You must provide a series of functions which meet the requirements in the table below. You you may include other functions as long as the requested functions execute correctly. Do not include a main function in your source or header files. You...

  • Using C++ Write two `void` functions.  These functions each take two  integer parameters.  The functions accomplish the following tasks....

    Using C++ Write two `void` functions.  These functions each take two  integer parameters.  The functions accomplish the following tasks. The first function prints out the numbers from the first argument up to and including the number passed as the second argument counting by the first argument (i.e. if the arguments are 2 and 10, the information below was given: #include <iostream> using namespace std; // function definitions// // main program int main() {   int firstNumber,secondNumber;   char countType, doAgain;   // we will do this...

  • using C , comments will be appreciated. Question1 Write down an function named bitwisedFloatCompare(float number1, float...

    using C , comments will be appreciated. Question1 Write down an function named bitwisedFloatCompare(float number1, float number2)that tests whether a floating point number number1is less than, equal to or greater than another floating point number number2, by simply comparing their floating point representations bitwise from left to right, stopping as soon as the first differingbit is encountered. The fact that this can be done easily is the main motivation for biased exponent notation. The function should return 1 if number1...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT