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
How much cell phone data does the average person use a month
azamat
It all depends on what you're doing online.
7 0
2 years ago
Y’all think Super Drags is good?
Kryger [21]

Yeah, I'm into it. It does show a lot of stereotypical views on drag queens, and it goes a little over the top, but honestly? The LGBT community has spent so long acting like the general population, and we're expected to be a sort of cookie cutter outline of the ideal person in order to fit in. We're not really allowed to be silly and have fun, otherwise we just get labeled as a stereotype, which sucks. When you're queer, you get labeled as that before anything else: your interests are seen as a byproduct of your queerness, not as an interest. So Super Drags, is actually a nice sort of change of pace. It's silly, it shows that queer people are human, and it sorta shows that "Yass bih" look on life, which is hilarious imo. Plus hey, Brazilian LGBT show that doesn't spout homophobic propaganda and supports diversity within all aspects of life? I'll support that.

TLDR; There aren't many silly shows out there that have an LGBT cast. Like, it's always supposed to be grim and sad, and all about heartbreak and coming out, yadda yadda yadda. So, it's cool that we've finally got something lighthearted.

3 0
3 years ago
Choose the list of the best uses for word processing software.
Marina86 [1]
List, resumes, spread sheet, databases, contracts
4 0
3 years ago
Examples in types of experimental methods <br>​
Elodia [21]

Lab Experiment

Field Experiment

Natural Experiment.

3 0
2 years ago
Read 2 more answers
Serena is adding headers and footers in the Slide Master view using the Header and Footer dialog box What is not
sergiy2304 [10]

Answer:

NUMBER 3

Explanation:

5 0
3 years ago
Read 2 more answers
Other questions:
  • It is safe to use your bright headlights if there is a car ahead of you within 300 feet
    9·2 answers
  • What is the solubility of an empty soda can
    10·1 answer
  • What is one invention that has had an impact on the way you live?<br><br>Write an essay
    15·2 answers
  • Many software makers provide free downloadable updates, sometimes called a(n) ______ to users who have registered and/or activat
    5·1 answer
  • What is online school like 3 sentences
    8·2 answers
  • PLEASE HELP!! Computer science test
    11·1 answer
  • In Python which is the correct method to load a module math?
    10·1 answer
  • Describe who was Alan Turing in 100 words.<br> First to answer will get brainliest.
    7·1 answer
  • "write a program to play and score the paper-rock-scissor game. each of two users types in either p, r, or s. the program then a
    6·1 answer
  • When Brittany practiced her presentation, she gave a trusted adult a presentation checklist to use to critique her each time she
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!