Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Computer Engineering Expert

  We can also use the logical operators to numbers directly and  perform simple bit manipulation . The operators are

    &  Bitwise AND
    |  Bitwise OR
    ^  Bitwise exclusive or 
    ~  Bitwise one's complement i.e. NOT
    <<  Left shift
    >>   Right shift

Example
  A 8 bit number represents a coded function, bit  3-4 describes the operation to be performed on the number in bits 0- 2 and 5-7  the function i.e. 
 
    00    Add
    01    Subtract
    10     Divide
    11    Multiply
 
  Write a Program to extract the number and operation

Answer
We need to extract the bits 3-4, the answer is bit operators 
 
    Assume number is in the variable A i.e.  11011011
 
    Mask off the first number i.e. bit 0-2
    num1 = a & 0x07;        11011011 and
                00000111
                00000011
    
    Mask off the second number i.e. bit 5 -7
    num2 = a & 0xe0;        11011011 and
                11100000
                11000000
 
    We need to shift num2 down by 5 places i.e.
    num2 = num2 >>5;
                00000110

This could be done in one instruction i.e.
    num2 = (a & 0xe0) >> 5;
 
  Mask off the operation bits i.e. bit 4-5
    operation = (a & 0x18) >> 3;
 
  We can then use the switch statement to select each operation
  switch(operation)
  {
  case 0:  
    total = num1+num2;
    break;
  case 1: 
     total = num1-num2;
    break;
  case 2:  
    total = num1/num2;
    break;
  case 3:  
    total = num1*num2;
    break;
  }
Hence the entire program is 
  #include
  void main()
  { 
  char prompt;
  /*Author : Mr James Mc Carren 
  Company: Staffordshire University 
   Date: 26th August 2012 
  Version 1.0 
   Function : To show bit manipulation
   Modifications:   none*/
  int num1,num2,operation,total,a;
  printf("Please enter in the number\n\r");
  scanf("%x",&a);
  num1 = a & 0x07;  
  num2 = (a & 0xe0) >> 5 ;
  operation = (a & 0x18) >> 3;
  switch(operation)
  {
  case 0:   total = num1+num2;
    break;
  case 1:  total = num1-num2;
    break;
  case 2:  total = num1/num2;
    break;
  case 3:  total = num1*num2;
    break;
  }
  printf("The total is %d\n\r",total); 
  printf("Press and key to exit \n\r");
  scanf("\n%c",&prompt);
  }

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M9537823

Have any Question?


Related Questions in Computer Engineering

A firm produces product a and product b this years sales

A firm produces Product A and Product B. This years sales price of Product A have decreased tremendously, and the sale of Product B has increase by 10 percent. The firm has threeemployees that can produce Product A and f ...

Listen to or read the transcript of this podcast

Listen to (or read the transcript of) this podcast (https://www.stlouisfed.org/education/economic-lowdown-podcast-series/episode-16-elasticity-of-demand) from the Federal Reserve Bank of St. Louis. describe your experien ...

A 1000 utility bond with 14 years remaining before maturity

A $1000 utility bond with 14 years remaining before maturity can now be purchased for $760. It pays interest of $20 each 6-month period. What rate of return is earned by purchasing the bond at the current market price pl ...

Lab assessment questions 1 what is the one thing that a

Lab Assessment Questions: 1. What is the one thing that a virus, a worm, spyware, and malicious code have in common? What are the differences among these three threats: a virus, a worm, and spyware? 2. How often should y ...

Are there more children diagnosed with autism spectrum

Are there more children diagnosed with Autism Spectrum Disorder (ASD) in states that have larger urban areas over states that are mostly rural? In the state of Pennsylvania, a fairly urban state, there are 245 eight year ...

Question imagine you are a junior researcher at a

Question: Imagine you are a junior researcher at a world-renowned, high-tech laboratory that receives millions of dollars in government grants each year. You have been assigned the task of writing a proposal for the DARP ...

What is the purpose of load balancing i need full

What is the purpose of load balancing? (I need full explanation) I already know what load balancing is, I just need to know why we use it

The formulas a rarr b and c and a rarr b and a rarr c are

The formulas A → (B ∧ C) and (A → B) ∧ (A → C) are logically equivalent. For this problem, you are going to prove that they are equivalent in two different ways. Prove that A → (B ∧ C) ≡ (A → B) ∧ (A → C) by writing two ...

You are required to carry out an analysis of the it

You are required to carry out an analysis of the IT requirements of a software company and design a virtualisation solution to satisfy these requirements. You will need to produce a report outlining your design and provi ...

Scenario 1 19500 ascii characters are transmitted over a 3

Scenario 1: 19,500 ASCII characters are transmitted over a 3 Mbps circuit. The protocol uses 8-bits to encode each ASCII character, and adds an additional parity bit to help detect errors. Calculate the transmission effi ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As