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.:- M9620345

Have any Question?


Related Questions in Computer Engineering

Question suppose that x 00271 and y 608 are correctly

Question : Suppose that x* = 0.0271 and y* = 6.08 are correctly rounded versions of x and y to 3 digit mantissas. The smallest interval [a,b] that would contain x is [0.02705, 0.02715] since 0.0271 is between those numbe ...

Explain why a u s recession that occurs as the rest of the

Explain why a U. S. recession that occurs as the rest of the world is expanding will tend to reduce the U. S. Trade deficit.

Recall that for a block cipher a key schedule algorithm

Recall that for a block cipher, a key schedule algorithm determines the subkey for each round, based on the key K. Let K = (K0,K1,K2......K55) be a 56-bit DES key. a. List the 48 bits for each of the 16 DES subkeys K1, K ...

This requires to use minitabthe following are the results

This requires to use Minitab The following are the results from a 2 5  design: Treatment       Response            Treatment       Response (1)                    700                     e                   800 a         ...

Question suppose you are given a set of cities p number of

Question : Suppose you are given a set of cities (p number of cities) and there longitude and latitude coordinates. You need to determine the two closest cities. Write an algorithm to determine the two closest cities. As ...

Kirk inc has come out with a new and improved product and

Kirk Inc. has come out with a new and improved product, and is expected to have an ROE of 14.4%. It will maintain a plowback ratio of 30%. Investors expect a 6.3% rate of return on the stock. (a) At what P/E ratio would ...

Review the interactive session on turner broadcasting and

Review the Interactive Session on Turner Broadcasting and e-commerce in the Management Information Systems: Managing the Digital Firm on pages 381-382. Then write a short paper (400 to 800 words) that answers all four Ca ...

The standard math library cmath includes a function for

The standard math library cmath includes a function for taking the square root of a number. The heading (prototype) for this function is: double sqrt(double x) Write another function called closer_root that takes two rea ...

This chart lists the estimated lengths and arrival times

This chart lists the estimated lengths and arrival times for 5 jobs. In what order do these run using FCFS? What is the average turn-around time (difference in time between a job's arrival and its completion)? In what or ...

We have seen how dynamic arrays enable arrays to grow while

We have seen how dynamic arrays enable arrays to grow while still achieving constant-time amortized performance. This problem concerns extending dynamic arrays to let them both grow and shrink on demand. a) Consider an u ...

  • 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