Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please solve in C language.Code only the function void short2Char(short encodedNum, char *c) /* File bit_manipulation.c Purpose : contains helper functions for manipulating bits *

Please solve in C language.Code only the function void short2Char(short encodedNum, char *c)

image text in transcribed

/*

File bit_manipulation.c

Purpose :

contains helper functions for manipulating bits

* /

/************************************************************************/

// INCLUDE FILES

#include "bit_manipulation.h"

/************************************************************************/

//MACROS / DEFINES

/************************************************************************/

// FUNCTION PROTOTYPES

/***************************************************/

//example of a testing function for the functions in the file

// it should be used only when adding code to the functions

// to use it:

// 1. change the statment below (preprocessing directive) to #if 1.

// This "tells" the compiler to include the code between #if 1 and #endif.

// 2. compile the file

// 3. exectute the file

// 4. if needed add more test calls

// 5. when you are satified with the results change the statement #if 1 to #if 0 to exclude the code from compilation.

#if 0

int main(int argc, char *argv[])

{

int rc = 0; // return code

short test = 0;

short modified;

int bitNum; // bit number to be used

test = 0;

bitNum = 2;

modified = test;

flipBitShort(bitNum, &modified);

printf("flipBitShort: test=%d, flipping bit #=%d, answer should be=%d, modified test=%d ", test, bitNum, 4, modified);

rc = isShortBitSet(modified, 3);

printf("isShortBitSet: bit 3 in %d should not be set. Functions returned %d ", modified, rc);

rc = isShortBitSet(modified, 2);

printf("isShortBitSet: bit 2 in %d should be set. Function returned %d ", modified, rc);

test = 9;

rc = countBits(test);

printf("countBits: test = %d number of bits set to 1 should be 2. Function returend: %d ", test, rc);

getchar();

return(0);

}

#endif

/*************************************************************************************/

/* purpose: checks if a bit at position bitNum is set to 1

input:

c - the character to be checked

bitNum - the bit number to be checked (expected range 0-7)

return:

1 if the bit is set to 1

0 if the bit is not set to 1 (bit is 0)

*/

int isCharBitSet(char c, int bitNum)

{

// add code

}

/*************************************************************************************/

/* purpose: checks if a bit at position bitNum is set to 1

input:

num - the number to be checked

bitNum - the bit number to be checked

return:

1 if the bit is set to 1

0 if the bit is not set (bit is 0)

return:

none

*/

int isShortBitSet(short num, int bitNum)

{

// add code

}

/*************************************************************************************/

/* purpose: sets bit bitNum to 1 in *num to 1

input:

num - the address of the short integer

bitNum - the bit number to be checked

Output:

*num - the modified short

return:

none

*/

void setShortBit(int bitNum, short *num)

{

// add code

}

/*************************************************************************************/

/* purpose: sets bit bitNum in *c to 1

input:

c - address of character

bitNum - the bit number to be checked

output:

*c - the modified character

return:

none

--------------------------------

/*

File transmit.c

Purpose: simulate a transmission of characters over the network.

It converts a message given as an array of characters to an array of short integers that are sent over the network.

The converted message uses 1-bit error correction code.

As part of the simulation, the program also injects errors to the encoded message by randomally flipping one bit.

*/

/************************************************************************/

// INCLUDE FILES

#include "stdio.h"

#include "stdlib.h"

#include "string.h"

#include "assert.h"

#include "bit_manipulation.h"

/************************************************************************/

//MACROS / DEFINES

#define MAX_MSG_LENGTH 2048

#define P1_MASK 0xaa8 // 0b0101010101000

#define P2_MASK 0xcc8 // 0b0110011001000

#define P4_MASK 0x10e0 // 0b1000011100000

#define P8_MASK 0x1e00 // 0b1111000000000

/************************************************************************/

// FUNCTION PROTOTYPES

int readMessage(char *s, int len, int *numRead);

int char2Short(char c, short *encodedChar);

int setParityBits(short *num);

/************************************************************************/

int main(int argc, char *argv[])

{

int rc = 0; // return code

char s[MAX_MSG_LENGTH] = { '\0' }; // message

short encodedMsg[MAX_MSG_LENGTH];

int numRead = 0; // number of characters in the message

int i;

// read the message

printf("Please enter a message to transmit: ");

readMessage(s, MAX_MSG_LENGTH, &numRead);

// encode the message

for (i = 0; i

char2Short(s[i], &encodedMsg[i]); // embedd the char in a short int

setParityBits(&encodedMsg[i]); // set the parity bits

}

// add errors to mesage

for (i = 0; i

if (rand()%36000

int bit = rand() % 13;

flipBitShort(bit, &encodedMsg[i]);

}

}

// print the message

printf(" Transmitted message (short integers): ");

for (i = 0; i

printf("%d ", encodedMsg[i]);

}

printf(" ");

return(0);

}

/***********************************************************************************/

/* reads a message from the user

input

len - maximum length of mesage that should be read

output

s - contains the message

numRead - the number of characters in the message

assumption - s is valid

*/

int readMessage(char *s, int len, int *numRead)

{

int i;

int rc = 1;

*numRead = 0;

for (i = 0; i

rc = scanf("%c", &s[i]);

if (s[i] == ' ') break;

(*numRead)++;

}

return(0);

}

/*************************************************************************/

/* spreads bits 0-7 of the character c into bits 3,5,6,7,9,10,11,12 of the short encodedChar

input

character c

output

encodedChar - a short with the bits of c

*/

int char2Short(char c, short *encodedChar)

{

short mask = 0x0001;

int i;

int bitSet;

*encodedChar = 0;

for (i = 0; i

// check if bit i is set

bitSet = isCharBitSet(c, i);

// set the correct bit in the encoded character

if (!bitSet) continue;

switch (i) {

case 0:

setShortBit(3, encodedChar);

break;

case 1:

case 2:

case 3:

setShortBit(4 + i, encodedChar);

break;

case 4:

case 5:

case 6:

case 7:

setShortBit(5 + i, encodedChar);

break;

default:

printf("This should not happen !!!! ");

assert(0);

}

}

return(0);

}

/*************************************************************************************/

/* sets the parity bits in the variable num

input

num - the short numbe where parithy bits must be set

*/

int setParityBits(short *num)

{

int mask;

int sum;

// set parity bit P1

// expose only the bits that are related to parity bit 1 using P1_MASK

// and count the number of bits that are set to 1

// if the number of bits is odd then set parity bit P1

// set parity bit P2

// expose only the bits that are related to parity bit 2 using P2_MASK

// and count the number of bits that are set to 1

// if the number of bits is odd then set parity bit P2

// set parity bit P4

// expose only the bits that are related to parity bit 1 using P4_MASK

// and count the number of bits that are set to 1

// if the number of bits is odd then set parity bit P4

// set parity bit P8

// expose only the bits that are related to parity bit 1 using P8_MASK

// and count the number of bits that are set to 1

// if the number of bits is odd then set parity bit P8

return(0);

}

-----------------------------------

/*

File receive.c

Purpose: simulate the receiveing end of a message over the network.

The program converts a message encodded as an array of short integers into characters.

The input message uses 1-bit error correction code.

As part of the simulation, the program also corrects any 1-bit errors in the receiving message.

*/

/************************************************************************/

// INCLUDE FILES

#include "stdio.h"

#include "stdlib.h"

#include "string.h"

#include "assert.h"

#include "bit_manipulation.h"

/************************************************************************/

//MACROS / DEFINES

#define XSTR(A) STR(A)

#define STR(A) #A

#define MAX_INPUT_LENGTH 8192

#define MAX_MSG_LENGTH 2048

#define P1_MASK 0xaa8 // 0b0101010101000

#define P2_MASK 0xcc8 // 0b0110011001000

#define P4_MASK 0x10e0 // 0b1000011100000

#define P8_MASK 0x1e00 // 0b1111000000000

/************************************************************************/

// FUNCTION PROTOTYPES

void short2Char(short encodedChar, char *c);

void correctCode(short *num);

void readEncodedMessage(short *buf, int len, int *numRead);

/**************************************************************************/

int main(int argc, char *argv[])

{

int rc = 0; // return code

char s[MAX_MSG_LENGTH] = { '\0' }; // message

short encodedMsg[MAX_MSG_LENGTH];

int numRead = 0; // number of characters in the message

int i;

// read the message

printf("Enter the transmitted message: ");

readEncodedMessage(encodedMsg, MAX_MSG_LENGTH, &numRead);

// print the corrected message

printf(" Transmitted Message: ");

for (i = 0; i

char c;

short2Char(encodedMsg[i], &c);

printf("%c", c);

}

printf(" ");

// correct the message

for (i = 0; i

correctCode(&encodedMsg[i]);

}

// print the corrected message

printf(" Corrected Transmitted Message: ");

for (i = 0; i

char c;

short2Char(encodedMsg[i], &c);

printf("%c", c);

}

printf(" ");

return(0);

}

/***********************************************************************************/

/* reads a message from the user

input

len - maximum length of mesage that should be read

output

receivedMsg - contains the message

numRead - the number of characters in the message

Asumption:

function does not do any error checking of the input. It expects to receive

a sequence of positive short integers.

*/

void readEncodedMessage(short *receivedMsg, int len, int *numRead)

{

int i;

int rc;

char s[MAX_INPUT_LENGTH+1];

char *token = NULL;

*numRead = 0;

s[MAX_INPUT_LENGTH] = '\0';

scanf("%"XSTR(MAX_INPUT_LENGTH)"[^ ]s", s);

token = strtok(s, " ");

for (i = 0; token != NULL && i

rc = sscanf(token, "%hd", &receivedMsg[i]);

}

*numRead = i; // set the number of read integers

// empty the input buffer

for( ; getchar() != ' '; );

}

/*************************************************************************/

/* assign bits 3,5,6,7,9,10,11,12 of a integer to bits 0-7 of a char

input

encodedNum - a short integer

output

*c - the modified char that is embedded in encodedNum

return:

none

comment:

this is the "inverse" of the function char2Short in the transmit program

*/

void short2Char(short encodedNum, char *c)

{

short mask = 0x0001;

int i;

int bitSet;

*c = 0; // initialize *c

for (i = 0; i

// for each bit i do

// if the corrsponding bit in the encodedNum is set then set bit i at the char c

// for example if i == 0 then if bit 3 in encodedNum is set to 1 then set bit 0 in c to 1

}

}

/*************************************************************************************/

/* purpose:

performs error corrrections, if needed on the short integer

The function checks the parity bits for error and see if thre is a discrepancy between

the transmitted parity bits and the computed parity bits for the received number.

If there is a decrepancy then it finds the bit number that was flipped.

If there is an error then it fixes it.

input

num - the address of the short number

output:

*num - the modified number

*/

void correctCode(short *num)

{

int mask;

int sum;

int bitNumber = 0; // bit number with the error bit

int parity; // a parity bit either 0 or 1

int storedParity; // the parity bit in the encoded char

// check parity bit p1

// expose only the bits related to P1 using P1_MASK and count the number of bits that are set to 1

// determine if parity bit P1 should have been set

// get the parity for P1_MASK that is stored in *num

// if the stored parity and newly computed parity are are different add 2^0 to bitNumber

// simlilary check parity bit p2

// calculate the parity for P2

// compare the calculated parity with the stored parity

// if the two parities are different add 2^1 to bitNumber

// check parity bit p4

// calculate the parity for P4

// compare the calculated parity with the stored parity

// if the two parities are different add 2^2 to bitNumber

// check parity bit p8

// calculate the parity for P8

// compare the calculated parity with the stored parity

// if the two parities are different add 2^3 to bitNumber

// if bitNumber != 0 then flip the bit at position bitNumber (use xor operator)

}

Receive program (45 pts) The provided code can already read the encoded message that is provided into an aray of integers. The program uses the output from the Transmit program as input to the receivie program. Using the provided skeleton do the following 1 (20 pts) Code the function void short2Char(short encodedNum, char *c) for unpacking the bits from the encoded short integer into a character. Namely, it takes the short integers encodedChar and maps bits 3,5,6,7,9,10,11,12 of the encodedChar onto bits 0,1,2,3,4,5,6,7 of c. Review the code of the function char2Short0 in the Transmit program

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions

Question

Create a decision tree for Problem 12.

Answered: 1 week ago