Question

C programming lab: Description: In this lab you will write a program that will contain two...

C programming lab:

Description:
In this lab you will write a program that will contain two functions, setlsbs() and getlsbs(). These functions will use bitwise operators to embed and extract "hidden" bits in a character array.

Write a program to test your functions as follows:

  • Obtain a random number seed from the command line of your program using command line arguments.

  • Initialize an array p of 8 unsigned char with random numbers from 0 to 255

  • Initialize a separate unsigned character byte0 with a random number.

  • Print the values in the array p as well as the value for byte0.

    • Print the values in decimal format as well as binary format (use macros defined below)

  • Call setlsbs() using p and byte0 as parameters

  • After the call to setlsbs() is completed, print the modified values of the array p.

    • Print the values in decimal format as well as binary format (use macros defined below)

  • Use the modified array p as a parameter to getlsbs()

  • Print the return value of the call to getlsbs(). The returned value should match the original value for byte0

    • Print the value in decimal format as well as binary format (use macros defined below)

Background Preparation:

  • Review bitwise operators (Section 2.9 in textbook).

Specifications:
Your first function:

void setlsbs(unsigned char *p, unsigned char b0)

will take as parameters, an array p of eight bytes (unsigned char) and a byte byte0. It will replace the least significant bits (LSBs) of p by the bits of byte0. In other words, if the binary representation of byte0 is b7b6b5b4b3b2b1b0, you should replace the LSB of p[0] by b0, the LSB of p[1] by b1, ... , and the LSB of p[7] by b7.

Your second function:

unsigned char getlsbs(unsigned char *p)

will take an array p of eight bytes (unsigned char) and return a byte byte0 which is created by combining the LSBs of p. That is, your function should combine the least significant bits bi of p[i] to return a byte b7b6b5b4b3b2b1b0.

Macros:
You may use the following macros to print the binary representation of unsigned character variables:

#define BYTETOBINARYPATTERN "%d%d%d%d%d%d%d%d"

#define BYTETOBINARY(byte) \
(byte & 0x80 ? 1 : 0), \
(byte & 0x40 ? 1 : 0), \
(byte & 0x20 ? 1 : 0), \
(byte & 0x10 ? 1 : 0), \
(byte & 0x08 ? 1 : 0), \
(byte & 0x04 ? 1 : 0), \
(byte & 0x02 ? 1 : 0), \
(byte & 0x01 ? 1 : 0)

#define PRINTBIN(x) printf(BYTETOBINARYPATTERN, BYTETOBINARY(x));

You can use the macros in a manner similar to the code below:

unsigned char num =173;

PRINTBIN(num); printf("\n");

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Do like and comment if you have any queries.

Note:

Used and updated the code given in the question and used the same variable names as requested

Code to copy:

   #include<stdio.h>
   #include<stdlib.h>

   #define BYTETOBINARYPATTERN "%d%d%d%d%d%d%d%d"

   #define BYTETOBINARY(byte) \
   (byte & 0x80 ? 1 : 0), \
   (byte & 0x40 ? 1 : 0), \
   (byte & 0x20 ? 1 : 0), \
   (byte & 0x10 ? 1 : 0), \
   (byte & 0x08 ? 1 : 0), \
   (byte & 0x04 ? 1 : 0), \
   (byte & 0x02 ? 1 : 0), \
   (byte & 0x01 ? 1 : 0)
   #define PRINTBIN(x) printf(BYTETOBINARYPATTERN, BYTETOBINARY(x));

   //funtion prints array p in binary format
   void print_Binary_parray(unsigned char *p){
   int i;
   for(i=0;i<8;i++){
       PRINTBIN(p[i]);
       printf("\n");
   }
   }
   //funtion prints array p in decimal format
   void print_Decimal_parray(unsigned char *p){
   int i;
   for(i=0;i<8;i++){
       printf("%u ",p[i]);
   }
   printf("\n");
   }
   //funtion sets the LSB of each element in array p wiht byte_0 value
   void setlsbs(unsigned char *p, unsigned char b0){
       unsigned char p_Tempy;
       unsigned char tst = 0x01;
       int i;
       int v;
  
       for(i=0;i<8;i++){
           p_Tempy = b0;
           //checking byte_0 bit is set at a particular position i
           v = p_Tempy&tst?1:0;
           //if the byte_0 bit is set in position i
           //set the p[i] LSB
           //else unset p[i] LSB
           if(v){
               p[i] = p[i] | 0x01;
           }else{
               p[i] = p[i] & 0xFE;
           }
           //moving the position decider of byte_0 to right
           tst = tst<<1;
       }
  
   }
   //funtion gets LSB bits of elements in arra p and append it to result
   unsigned char getlsbs(unsigned char *p){
   int i;
   unsigned char res=0;
   for(i=7;i>=0;i--){
       //appending res with LSB of p[i]
       //the shift right the result
       res=(res|(p[i]&0x01));
       if(i!=0)
       res=res<<1;
   }
   return res;
   }

   int main(){
   int i;
   int max=255;
   int min=0;
   //setting the seed of random number generation
   srand(1);
  
   unsigned char byte_0 ;
   unsigned char p[8];
   //Initialize an array p of 8 unsigned char with random numbers from 0 to 255
   for(i=0;i<8;i++){
       p[i]=(rand() % (max - min + 1)) + min;
   }
   //Initialize a separate unsigned character byte_0 with a random number.
   byte_0 = rand();
  
   //Printing the values in the array p as well as the value for byte_0. in decimal format
   printf("\nValue of array P in decimal format= ");
   print_Decimal_parray(p);
   printf("\nValue of byte_0 in decimal format = %u\n",byte_0);
  
   //Printing the values in binary format
   printf("\nValue of array P in binary format\n");
   print_Binary_parray(p);
   printf("\nValue of byte_0 in binary format =");
   PRINTBIN(byte_0);
  
   //Calling setlsbs() using p and byte_0 as parameters
   setlsbs(p,byte_0);
  
   //printing modified values of array p in decimal
   printf("\nPrinting P after setting LSB in decimal = ");
   print_Decimal_parray(p);
  
   //print the modified values of the array p. in binary
   printf("\nPrinting P after setting LSB in binary\n");
   print_Binary_parray(p);
  
   //calling getlsb() with porameter p
   unsigned char byte_0_new = getlsbs(p);

   //Returned value of byte_0 from getlsb() in decimal format
   printf("\nValue of returned byte_0 in decimal format = %u\n",byte_0_new);
  
   //Returned value of byte_0 from getlsb() in binary format
   printf("\nValue of returned byte_0 in binary format =");
   PRINTBIN(byte_0_new);
   return 0;
   }

Output Screenshot:

Code Screenshot:

Add a comment
Know the answer?
Add Answer to:
C programming lab: Description: In this lab you will write a program that will contain two...
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
  • Write a program that allows the user to enter an unsigned integer (the maximum value of...

    Write a program that allows the user to enter an unsigned integer (the maximum value of an unsigned 4-byte int is 232 = 4,294,967,296) and reverses its format (from little to big endian, or vice versa). Print out the user-entered number in hexadecimal and binary, reverse the endianness, and print the reverse in hexadecimal and binary. Integers in most machine architectures are represented in little endian format: the least significant byte is stored in the smallest address; for instance, the...

  • (Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byt...

    (Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byte unsigned int variable. Write a program that inputs four characters from the keyboard and passes them to function packCharacters. To pack four characters into an unsigned int variable, assign the first character to the unsigned intvariable, shift the unsigned int variable left by 8 bit positions and combine the unsigned variable with the second character using the bitwise inclusive OR operator....

  • You must write a C program that prompts the user for two numbers (no command line...

    You must write a C program that prompts the user for two numbers (no command line input) and multiplies them together using “a la russe” multiplication. The program must display your banner logo as part of a prompt to the user. The valid range of values is 0 to 6000. You may assume that the user will always enter numerical decimal format values. Your program should check this numerical range (including checking for negative numbers) and reprompt the user for...

  • Write a C program that uses the bitwise shift operators to shift the bits to the...

    Write a C program that uses the bitwise shift operators to shift the bits to the right >> or the left > m; /* This shifts m bits to the right, and the m least significant bits are lost.*/ The following statements are the same. num = num >> 3; num >>= 3; Show the operation in binary by calling the following function as defined in 3.1, void to_binary(unsigned int n); The function converts decimal to binary and outputs the...

  • C++ Programming - Design Process I need to create a flow chart based on the program...

    C++ Programming - Design Process I need to create a flow chart based on the program I have created. I am very inexperienced with creating this design and your help will be much appreciated. My program takes a string of random letters and puts them them order. Our teacher gave us specific functions to use. Here is the code: //list of all the header files #include <iomanip> #include <iostream> #include <algorithm> #include <string> using namespace std; //Here are the Function...

  • In C Programming Language, write a program Character Pointers and Functions. Keyboard input to enter one...

    In C Programming Language, write a program Character Pointers and Functions. Keyboard input to enter one character string. Using a single dimension array, populate the array with the character string, call a function using pointers to reverse order the character string, pass back to the main the output and total_count_of_characters. (maybe use a global variable for the total count). Print display the reversed char string and total_count.

  • In C Programming Language, write a program Character Pointers and Functions. Keyboard input to enter one...

    In C Programming Language, write a program Character Pointers and Functions. Keyboard input to enter one character string. Using a single dimension array, populate the array with the character string, call a function using pointers to reverse order the character string, pass back to the main the output and total_count_of_characters. (maybe use a global variable for the total count). Print display the reversed char string and total_count.

  • C PROGRAM When you print out the old and new bitsets, which are of type unsigned...

    C PROGRAM When you print out the old and new bitsets, which are of type unsigned char, please use the %p control character in the printff statement rather than %x or %d or %c. The compiler will complain, but we don't always listen to them anyway. The difference is it will print the bitset out in hex with a preceeding %0x. unsigned char bitset = 0x14 ; printf("Bitsrt is %p\n", bitset) ; results in Bitset is 0x14, which is what...

  • C- PROGRAMMING PROJECT #4 Design and Write a C program to calculate an average of an...

    C- PROGRAMMING PROJECT #4 Design and Write a C program to calculate an average of an array of numbers and produce the following output: 1. Your first and last name 2. Course Number 3. C Project Number 4. All the numbers in the array 5. The sum of all the numbers in the array 6. The count of all the numbers in the array 7. The average of all the numbers in the array Double-space after lines 3, 4, 5,...

  • PRG255 3.2 (2 marks) Write a C program that uses the bitwise shift operators to shut...

    PRG255 3.2 (2 marks) Write a C program that uses the bitwise shift operators to shut the The program will ask the user to enter an unsigned also how many bits for the shift operation. Display the entered operation in both decimal and binary formats, vise shirt operators to shift the bits to the right >> or the left << user to enter an unsigned integer number, choose right shift or left shift, and orauon. Display the entered number and...

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