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]
2 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]2 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
Write a program that reads in an integer, and breaks it into a sequence of individual digits. Display each digit on a separate l
Montano1993 [528]

Answer:

The program in Python is as follows:

num = int(input())

for i in str(num):

   print(int(i))

Explanation:

This gets input for the number

num = int(input())

This converts the number to string and iterates through each element of the string

for i in str(num):

This prints individual digits

   print(int(i))

4 0
2 years ago
The first row in a table is referred to as the _____ row and the last row is considered the _____ row.
Juliette [100K]

the first row in a table is classed as the header row.

and with the last one I'm not sure because as far as I know there's not considered a last row.

6 0
3 years ago
(tco 7) the asp.net ajax client-side framework is loaded on the _____ for an asp.net web application.
jok3333 [9.3K]

a client-side framework is loaded on the client side, ie., the browser.

4 0
2 years ago
Everybody at a company is assigned a unique 9 digit ID. How many unique IDs exist?
Brut [27]
If you include all zeroes, then it should be 1,000,000,000. Otherwise it’s 999,999,999.

Think of how between 0-9 theres 10 numbers, and use that logic towards 000000000-999999999
3 0
3 years ago
What is powerpoint in a presentation?
Hitman42 [59]
Powerpoint is slides to your subject, whatever your sub is you can go to google slides and make you powerpoint there. you might have make a email account im not sure.

hope this helped!
4 0
2 years ago
Read 2 more answers
Other questions:
  • For film editors, which task comes last in their workflow pattern?
    12·2 answers
  • When the tcp\ip translates a network layer address into a data link layer address (after not finding an entry for the network la
    13·1 answer
  • At an open or uncontrolled intersection, yield if ____.
    15·2 answers
  • _____ consists of the instructions that direct the operation of the computer system and enable users to perform specific tasks,
    15·1 answer
  • If a tv was showing a flat black or blue screen or had "snow", what steps would you take to fix it? Give an example of the direc
    11·1 answer
  • The hardware to keep the output data when finished is a
    9·1 answer
  • Midday is a good time to take a portrait outside.<br> true or false?
    8·1 answer
  • 15. Virus cannot infect files that are saved in i. USB ii. CD-ROMs iii. Memory card iv. All of them​
    14·2 answers
  • SINCE I CANT SEE IT KANG LOOK
    6·2 answers
  • Which device is used to direct the flow of data on a computer network?
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!