Question

You will need to fill in the body of setLeftBit(). The input will be a byte...

You will need to fill in the body of setLeftBit(). The input will be a byte (i.e., 8 bits) input and a single bit bit, and you will need to set the leftmost bit of input to bit and return the result.

For example, if input is 127 (i.e., 01111111) and bit is 1, the result would be 255 (i.e., 11111111).

Please do the solution in C++ and explain/comment your code. Thanks.

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

#include <iostream>
#include <cmath>

using namespace std;

int Number;
int arrDig[8] = {128, 64, 32, 16, 8, 4, 2, 1};
int arrCheck[7];
int iTotal = 0;

int setLeftBit(int Number){
if(Number < 0 || Number > 255)
   {
       return 0;
   }

   else

   {
       for(int i=0; i<7; i++)
       {
           if(Number >= (iTotal + arrDig[i]))
           {
               {
                   arrCheck[i] = 1;

                   iTotal = iTotal + arrDig[i+1];


                   if(Number >= iTotal)
                   {
                       arrCheck[i+1] = 1;
                   }

                   else

                   {
                       arrCheck[i] = 0;
                   }
               }
           }

               else

           {
               {
                   {
                       {arrCheck[i] = 0;}
                   }
               }
           }
       }
       cout<<"Before Set Leftmost-bit..";
       cout << arrCheck[0] << arrCheck[1] << arrCheck[2] << arrCheck[3] << arrCheck[4] << arrCheck[5] << arrCheck[6] << arrCheck[7] << "\n" << endl;
   }

   if(arrCheck[0]==1){
arrCheck[0]=0;
cout<<"After Set Leftmost-bit..";
cout << arrCheck[0] << arrCheck[1] << arrCheck[2] << arrCheck[3] << arrCheck[4] << arrCheck[5] << arrCheck[6] << arrCheck[7] << "\n" << endl;
   }
   if(arrCheck[0]==0){
arrCheck[0]=1;
cout<<"After Set Leftmost-bit..";
cout << arrCheck[0] << arrCheck[1] << arrCheck[2] << arrCheck[3] << arrCheck[4] << arrCheck[5] << arrCheck[6] << arrCheck[7] << "\n" << endl;
   }

}

int main()
{
   cout << "Enter the Number: " << endl;
   cin >> Number;

setLeftBit(Number);
   return 1;
}

Add a comment
Know the answer?
Add Answer to:
You will need to fill in the body of setLeftBit(). The input will be a byte...
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
  • Could you please help me with an answer to this question in a simple way for...

    Could you please help me with an answer to this question in a simple way for the beginner? This is a beginner level class so can you please help me. The code must have beginner level implementation. Can you also please explain what things do. Thank you! Write a program that prompts the user for a positive integer number smaller than 256 (i.e. in [0...255]) and converts it into an 8-bit binary number. The bits are the remainders of the...

  • .MODEL FLAT INCLUDE io.h i header file for input/output STACK 4096 .DATA word1 word 1234 result byte 6 dup(?), e .C...

    .MODEL FLAT INCLUDE io.h i header file for input/output STACK 4096 .DATA word1 word 1234 result byte 6 dup(?), e .CODE MainProc PROC wtoa result, word1 exit with return code e mov eax, ret MainProc ENDP END Assume you set a break point in ret statement and look at the memory that starts from the address of result. Write what your expect to see in the next 6 bytes starting from result. .MODEL FLAT INCLUDE io.h i header file for...

  • Application Problem: Answer the following questions at the bottom of the worksheet: You are configuring a...

    Application Problem: Answer the following questions at the bottom of the worksheet: You are configuring a microcontroller (uC) to sample a signal connected to an input pin. Part of the initial setup requires that you clear (turn off) bits #17 and #2 in a 32-bit register, while leaving all other bits unchanged. To work with specific bits, we typically use a second number, called a mask, which has the bit positions we need to alter set to 1, and all...

  • NETWORKING I please help.. thanks you. Given that a subnet mask byte value has bits set...

    NETWORKING I please help.. thanks you. Given that a subnet mask byte value has bits set to 1 from left to right there is a small set of possible values. The first tow values are 128,192. What are the rest of the values? Hint: here are place values or each bit in a byte 128           64          32           16            8            4         2          1 What are the rest of the values? 128, 192... Consider the following pc configuration IP address:                 192.168.1.2 Subnet...

  • This is a fill in the code type: // FILL IN THE CODE - Write a...

    This is a fill in the code type: // FILL IN THE CODE - Write a program to multiply 2 numbers, print out the results and print out the original numbers in ascending order. #include <iostream> using namespace std; int main() {   int num1;       // holds 1st number   int num2;       // holds 2nd number   int result;       // holds result of multiplication   int *num1Ptr = nullptr; // int pointer which will be set to point to the 1st number   int *num2Ptr...

  • I need help implementing the following code without using filehandling and only getchar(); this is the...

    I need help implementing the following code without using filehandling and only getchar(); this is the prompt "The input for your program will be a text file containing a large amount of English. Typically, an English sentence ends with a period (aka, dot). Many years ago, when people used mechanical typewriters, the proper form was to place one space between words in a sentence, but two spaces after the period at the end of the sentence. This rule is no...

  • You are given a homework processor (HPro) capable of addressing 32 8-bit (1 byte) wide registers....

    You are given a homework processor (HPro) capable of addressing 32 8-bit (1 byte) wide registers. However, it has only 29 physical registers. Register RO, R1 and R31 are not physically implemented. Instead, every read from RO, R1 and R31 will return a constant zero (00000000), constant one (00000001) and all ones (11111111), respectively. Every write to RO, R1 and R31 will go to null (dummy write). Assume that all other registers have initially unknown (X) state (This in fact...

  • A protocol is a set of rules that define some operation. For example, the Internet Protocol...

    A protocol is a set of rules that define some operation. For example, the Internet Protocol (IP) specifies how messages are routed throughout the internet. In this problem, you are asked to implement your 1st messaging protocol (MP1). MP1 is a binary protocol that is used to efficiently encode a set of arithmetic operations using a 4-byte data type (e.g., int). The specification of the protocol is as follow. Using a 4-byte (32-bit stream), MP1 messages encode the ‘+’, ‘-‘,...

  • Done in Java using Apache NetBeans IDE 11.2 -- Objectives: To create a data structure with...

    Done in Java using Apache NetBeans IDE 11.2 -- Objectives: To create a data structure with underlying linked chain; To utilize and implement code dealing with parity big codes for error detection; To read data from file **The other answer when searching for this question does not have a ParityChain class, a ParityChainDemo class or even include some of the stated requirements (such as hasError method). **Parity bit was specified by instructor to be LEFT most bit in chain In...

  • **I asked this question once, and the answer completely ignored a majority of the rules listed...

    **I asked this question once, and the answer completely ignored a majority of the rules listed on this. PLEASE read what is necessary before giving an answer. **NEITHER answer when searching for this question has a ParityChain class, a ParityChainDemo class or even include some of the stated requirements (such as hasError method). **Parity bit was specified by instructor to be LEFT most bit in chain Done in Java using Apache NetBeans IDE 11.2 -- Objectives: To create a data...

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