1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
soldier1979 [14.2K]
3 years ago
11

In this assignment, 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.
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.

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)

You will create a Makefile to compile and link your programs.

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");
Computers and Technology
1 answer:
kicyunya [14]3 years ago
6 0

Answer:

#include "Lab11_jlilly3_204.h"

#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));

//! Set the least significant bit for all values in an array to the coorrisponding bit pos. in b_o

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

{

int i;

for (i = 0; i < 8; i++)

{

int x = b_0 & 1;

// If x is one set the low order bit to 1 using or

if (x)

{

p[i] |= x;

}

// If the value is a 0 and with one's complement to set lo to 0

else

{

p[i] &= ~1;

}

// shift our bit to the right to get the next value in the bit

b_0 = b_0 >> 1;

}

}

//! Get the least significant bit from each position in an array and add it to a byte

unsigned char getlsbs(unsigned char *p)

{

int i;

unsigned char c = 0;

for (i = 0; i < 8; i++)

{

// find out what the lsb is

int x = p[i] & 1;

if (x)

{

// if the lsb was a one insert a one and shift it into position

c |= 1 << i;

}

}

return c;

}

//! Print a given array in decimal

void print_decimal(unsigned char* p)

{

int k;

for (k = 0; k < 8; k++)

{

printf("Num at %d: %d\n", k, p[k]);

}

}

//! Print a given array in in bianary representation

void print_bin(unsigned char* p)

{

int c;

for (c = 0; c < 8; c++)

{

printf("Num at %d ", c);

PRINTBIN(p[c]);

printf("\n");

}

}

You might be interested in
IF YOU COULD CREATE A SOCIAL NETWORK:, what would it be like? What would make it so special about the others? (If you want you c
frosja888 [35]

It would be like instagram

3 0
3 years ago
design a relational database in EER for bike helmets and their reviews. a bike helmet has a name and color attributes. a bike co
Law Incorporation [45]

Answer:something you gotta do on your own

Explanation:

7 0
3 years ago
U
beks73 [17]
8wmX si2 jkkajmid di e2 2i2
3 0
3 years ago
The students of a college have to create their assignment reports using a word processing program. Some of the questions in thei
ser-zykov [4K]

Answer:

Table function

Explanation:

The table function can also be used to compare between items. The items can be arranged in columns and the features tonbe compared can be placed in columns. With this one can make comparison between the items.

5 0
3 years ago
What is an icon or animation used to represent aparticipant in an internet chat referred to as?
Veronika [31]
Emoji is an icon or animation used to represent a participant in an internet chat.
8 0
3 years ago
Other questions:
  • The file containing the definitions of the member functions of class DateType is called the ______ file.
    6·1 answer
  • Operating system software allows you to use your fingers, a mouse or other pointing device to select screen controls, such as bu
    5·2 answers
  • In general, what is the leftmost point of a circle with radius r centered at (x,y)?
    8·1 answer
  • Which of the following statements about Linux is not​ true? A. Linux works on all the major hardware platforms. B. It plays a ma
    5·1 answer
  • Enum Digits {0, 1};
    10·1 answer
  • The function below takes a single argument: data_list, a list containing a mix of strings and numbers. The function tries to use
    6·1 answer
  • Using ________ as a promotion method will bring return visitors to your site.
    8·1 answer
  • Which of these statements about tags is false?
    12·1 answer
  • What is abbreviation of the computer​
    15·2 answers
  • Imagine running a 64-bit system on a 32-bit system, where we simulate a single 64- bit memory location (register) using two atom
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!