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
"As part of integrating your solution your client indicates that they have certain technologies enabled on their network that al
erastovalidia [21]

Answer:

quality of service

Explanation:

6 0
3 years ago
Write a program that generates 1,000 random integers between 0 and 9 and displays the count for each number. (Hint: Use a list o
masya89 [10]

Answer:

import random

count0, count1, count2, count3,

count4, count5, count6, count7,

count8, count9, i = [0 for _ in range(11)]

while i < 1000:

   number = random.randint(0,9)

   if number == 0:

       count0 = count0 + 1

   if number == 1:

       count1 = count1 + 1

   if number == 2:

       count2 = count2 + 1

   if number == 3:

       count3 = count3 + 1

   if number == 4:

       count4 = count4 + 1

   if number == 0:

       count5 = count5 + 1

   if number == 6:

       count6 = count6 + 1

   if number == 7:

       count7 = count7 + 1

   if number == 0:

       count8 = count8 + 1

   if number == 9:

       count9 = count9 + 1

   

   i = i+1

print("0's: "+ str(count0) + "\n"+ "1's: "+ str(count1) + "\n"+

"2's: "+ str(count2) + "\n"+ "3's: "+ str(count3) + "\n"+

"4's: "+ str(count4) + "\n"+ "5's: "+ str(count5) + "\n"+

"6's: "+ str(count6) + "\n"+ "7's: "+ str(count7) + "\n"+

"8's: "+ str(count8) + "\n"+ "9's: "+ str(count9) + "\n")

Explanation:

- Initialize variables to hold the count for each number

- Initialize <em>i</em> to control the while loop

- Inside the while loop, check for the numbers and increment the count values when matched.

- Print the result

5 0
3 years ago
Write a complete Java program called Rooter that gets a positive integer called "start" from the user at the command line and th
Nataly_w [17]

Answer:

The program in Java is as follows:

import java.util.*;

import java.lang.Math;

public class Rooter{

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

 int start;

 System.out.print("Start: ");

 start = input.nextInt();

 while(start<=0){

     System.out.print("Number must be positive\nStart: ");

     start = input.nextInt();  }

 while(start>=0){

     System.out.println(Math.sqrt(start));

     start--;  }

}

}

Explanation:

This declares start as integer

 int start;

This prompts the user for input

 System.out.print("Start: ");

This gets input for start

 start = input.nextInt();

The following is repeated until the user input is valid i.e. positive

<em>  while(start<=0){</em>

<em>      System.out.print("Number must be positive\nStart: ");</em>

<em>      start = input.nextInt();  }</em>

The following while loop prints the square root of each number till 0

<em>  while(start>=0){</em>

<em>      System.out.println(Math.sqrt(start));</em>

<em>      start--;  }</em>

4 0
2 years ago
1. Add the following method to the Point class:
Pachacha [2.7K]

Answer:

Explanation:

The following code is written in Java and creates all of the methods that were requested in the question. There is no main method in any of these classes so they will have to be called from the main method and call one of the objects created method for the code to be tested. (I have tested it and it is working perfectly.)

class Point {

   private int x, y;

   public void Point(int x, int y) {

       this.x = x;

       this.y = y;

   }

   public double distance (Point other) {

      double distance = Math.sqrt(Math.pow((other.x - this.x), 2) + Math.pow((other.y - this.y), 2));

      return distance;

   }

   

   public int quadrant() {

       if (this.x > 0 && this.y > 0) {

           return 1;

       } else if (this.x < 0 && this.y > 0) {

           return 2;

       } else if (this.x < 0 && this.y < 0) {

           return 3;

       } else if (this.x > 0 && this.y < 0) {

           return 4;

       } else {

           return 0;

       }

   }

}

class Name {

   String firstName, lastName;

   char middleInitial;

   

   public String getNormalOrder() {

       String fullName = firstName + " " + middleInitial + " " + lastName;

       return fullName;

   }

   

   public String getReverseOrder() {

       String fullName = lastName + ", " + firstName + " " + middleInitial;

       return fullName;

   }

}

4 0
2 years ago
What should you do if your temperature gauge moves up to just below the red zone?
Alexeev081 [22]
<span>If the temperature gauge moves up to just below the red zone,you should turn off your air conditioner and turn on your vehicle's heater. Then immediately </span>find a mechanic or pull over safely and contact a road service.




4 0
3 years ago
Read 2 more answers
Other questions:
  • C2.5 - A group of four pirates has a treasure chest and one unique lock and key for each pirate. Using hardware that is probably
    6·1 answer
  • What are some characteristics of filtering junk email in Outlook 2016? Check all that apply.
    9·2 answers
  • Part cost Calculator Modify the GUI in your program to look like the following GUI. To do that you need to add the following to
    13·1 answer
  • All health information available on the internet is valid. <br> a. true <br> b. false
    6·1 answer
  • Name two materials that we can burn in order to get energy from biomass
    9·1 answer
  • What is the value of numX when this program is executed? if 3 &lt; 5 and 8 != 3: numX = 3 else: numX = 7
    13·2 answers
  • Every finger has a key it should be resting on when you are not typing<br> 1. False<br> 2. True
    12·2 answers
  • Can anyone please help me out
    10·1 answer
  • A while loop is frequently used to ______________ data.
    5·1 answer
  • Any changes done to the software during the operational phase of the software before project wind up is called as maintenance. S
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!