Write a c program that compiles and runs without errors

Assignment Help Computer Engineering
Reference no: EM132194673

Question :

Write a C program that compiles and runs without errors or warnings. This program has a bunch of little bit-manipulation functions in it. Your function prototypes should match the ones Igive here. Your main function should call each of your other functions. The purpose of your main function is to convince you that your other functions are working properly.

You may assume that your program is running in an environment that has 32-bit unsigneds and 64-bit unsignedlong longs, as the MS VS environment does.

When we speak of the position of a bit, let's follow the convention that the position of the lowest-order bit isposition 0. The rightmost (lowest-order) bit of an unsigned is bit #0, and the leftmost (highest-order) bit of anunsigned is bit #31.

Let's say that when we say bit field (12,7], we mean the collection of the following 5 bits:bit #7, bit #8, bit #9, bit #10, and bit #11 (not bit #12). The number on the right (next to the bracket) is theposition of the lowest-order bit in the bit field, while the number on the left (next to the parenthesis) is theposition of the first bit past the bit field. Bit field (5, 5] contains no bits. Bit field (6,5] contains one bit (namely,bit #5). Bit field (32.0] contains all 32 bits, starting with bit #0 and ending with bit #31.

As always, you are free to have your functions call other functions. On this assignment, you can save yourself significant time by doing this. It's smart to read through the assignment and see where you want to have your functions delegate some of their work to other functions.

0. void show( unsigned n );

Output the 32 bits of the arg, MSB first, with spaces separating the bytes, and a newline at the end.

(MSB = Most Significant Bit = highest-order bit,

LSB = Least Significant Bit = lowest-order bit)

For example, if main says

show( 0xBadDecafU );

then the output should be

10111010 11011101 11101100 10101111

1. void show64( unsigned long long nn );

Just like show, but now our arg is an unsigned long long. Recall that C (unlike C++) has no functionoverloading, so we have to give the 32-bit and 64-bit versions of the function different names.

For example, if main says

show64( 0xFeedFaceDeadBeefULL );

then the output should be

11111110 11101101 11111010 11001110 11011110 10101101 10111110 11101111

2. unsigned f0( unsigned n );

Returns the result of messing around with the bits of n in the following way:

Starting with n, turn on the bits in bit field (5,0]. (Recall that bit field (5,0] means bit #0, bit #1, bit #2, bit #3, and bit #4 (not bit #5).)

Flip the bits (10,5].

Turn off the bits (15,10].

Swap bit fields (20, 15] and (25, 20]. For instance, if bit field (20,15] held 11011 and bit field (25,20]held 01101, then after the swap bit field (20,15] would hold 01101 and bit field (25,20] would hold 11011.

Leave the bits (32, 25] the same as they were originally in n.

For example, f0( 0xBadDecafU ) returns 0xBBB6835FU:

n: 1011101 01101 11011 11010 00101 01111

f0(n): 1011101 11011 01101 00000 11010 11111

The function does no I/O, though of course main can output the returned value if it wants.

3. unsigned long long f1( unsigned long long nn );

(Similar to f0, but with 64-bit values)

Starting with nn, flip the bits in (8, 0].

Swap bit fields (16, 8] and (32, 24], leaving (24, 16] unchanged

Reset the bits in (40, 32] to 0.

Leave the bits in (56), 40] unchanged.

Set the bits in (64, 56] to 1.

For example, f1( 0xFeedFaceDeadBeefULL ) returns 0xFFEDFA00BEADDE10ULL:

nn: 11111110 11101101 11111010 11001110 11011110 10101101 10111110 11101111

f1(nn): 11111111 11101101 11111010 00000000 10111110 10101101 11011110 00010000

4. unsigned number1Bits( unsigned n );

Returns the number of 1-bits in n.

For example, number1Bits( 0xBadDecafU ) returns 22.


5. unsigned number1Bits64( unsigned long long nn );

Just like number1Bits, but this is the 64-bit version.

For example, number1Bits64( 0xFeedFaceDeadBeefULL ) returns 48.

6. unsigned number0Bits( unsigned n );

Same as number1Bits, but now we're returning the number of 0-bits instead of the number of 1-bits.

For example, number0Bits( 0xBadDecafU ) returns 10.

7. unsigned number0Bits64( unsigned long long nn );

64-bit version.

For example, number0Bits64( 0xFeedFaceDeadBeefULL ) returns 16.

8. unsigned lowBitPosition( unsigned n );

Returns the position of the lowest-order 1-bit in n.

For example, lowBitPosition( 0x0F0ABCC0U ) returns 6.

If there are no 1-bits, then the function should return 32.

9. unsigned lowBitPosition64( unsigned long long nn );

64-bit version.

For example, lowBitPosition64( 0xBABE000000000000ULL ) returns 49.

For example, lowBitPosition64( 0x00000000BABE0000ULL ) returns 17.

For example, lowBitPosition64( 0x0000000000000000ULL) returns 64.

10. unsigned lowBitValue( unsigned n );

Returns the place value (contribution) of the lowest-order 1-bit in n.

For example, lowBitValue( 0x000ABC00U ) returns 1024, which is 2^10.

If there are no 1-bits, then the function should return 0.

11. unsigned long long lowBitValue64( unsigned long long nn );

64-bit version.

For example, lowBitValue64( 0x0600000000000000ULL ) returns 144115188075855872, which is 2^57.

If there are no 1-bits, then the function should return 0.

Here are the 12 prototypes, so you can just copy & paste them into your program:
void show( unsigned n );

void show64( unsigned long long nn );

unsigned f0( unsigned n );

unsigned long long f1( unsigned long long nn );

unsigned number1Bits( unsigned n );

unsigned number1Bits64( unsigned long long nn );

unsigned number0Bits( unsigned n );

unsigned number0Bits64( unsigned long long nn );

unsigned lowBitPosition( unsigned n );

unsigned lowBitPosition64( unsigned long long n );

unsigned lowBitValue( unsigned n );

unsigned long long lowBitValue64( unsigned long long n );

Reference no: EM132194673

Questions Cloud

A program to calculates a vehicles gas mileage : The program should ask the user to enter the number of gallons of gas the vehicle can hold and the number of miles it can be driven on a full tank.
Write a description of the selected dataset and project : ITECH1103- Big Data and Analytics - write a brief reflection about this project in terms of challenges, learning and contribution
Calculate the monthly cost of a house given selling price : Write a C++ program to calculate the monthly cost of a house given the selling price, annual rate of interest, and number of years for the loan.
Market for housing as a result : Suppose that consumers begin to believe that the price of housing will be lower next period. What will happen in the market for housing as a result
Write a c program that compiles and runs without errors : Write a C program that compiles and runs without errors or warnings. This program has a bunch of little bit-manipulation functions in it.
Compute how many seconds are in 20 years : Write a C# program to convert one billion seconds (1,000,000,000 seconds) into years, months, days, hours, minutes, and seconds, using visual studio.
Calculate the gross pay of a set of employees : Calculate the gross pay of a set of employees. For each employee the program should prompt the user to enter the clock number.
Find increase in temperature for a thermally insulated chunk : Radioactive decay of granite and other rocks in Earth's interior provides sufficient energy to keep the interior molten, to heat lava, and to provide warmth.
Calculate an average of several integers entered by the user : Calculate an average of several integers entered by the user. It first prompts the user to enter the number of integers she wants to average.

Reviews

Write a Review

Computer Engineering Questions & Answers

  Write two test cases for each function to make sure

Create a function sum_abs(), that takes two arguments and returns the sum of the absolute values of both arguments.

  Developing the marketing requirements

Develop a code scheme that will meet marketing managers stated requirements. Write down a brief memo to marketing manager proposing at least one alternative to the code she proposed

  How do you think the hacker got into the computer to set

a hacker had installed a program on the computer that made it automatically send out tons of spam email without the computer owner's knowledge

  What is the difference between a operator and a function

What is the difference between a (binary) operator and a function? Is it possible using operator overloading to change the behavior of + on integers?

  Calculate the pearsons coefficient

Calculate the Pearson's coefficient again, now assuming that commercials need 1 week to create an effect, therefore shifting the values.

  Define an s - t cut for node-capacitated networks

In a standard s - t maximum flow problem, we assume edges have capacities, and there is no limit on how much flow is allowed to flow through a node.

  Computer platformscomputer systems may be classified into

computer platformscomputer systems may be classified into two categories classical and quantum. this assignment will

  Write a method called reverse that accepts string parameter

Write a Boolean method called startT that accepts a String parameter and returns true if the string starts with "T", otherwise returns false.

  Show a list of the three most recent visible threads sorted

Using the provided Thread class, show a list of the 3 most recent visible threads sorted to show the most recently created threads first.

  How will you search for a pattern printf

How will you search for a pattern printf and then repeat the search in the opposite direction from that in which the original search was made?

  Define what are the steps necessary to fix the problem

Part of implementing Ken 7 Windows' new enterprise resource planning (ERP) software is setting up a new recovery procedure for each type of computer.

  Describe how a typical lock manager is implemented

Describe how a typical lock manager is implemented - Why must lock and unlock be atomic operations and what is the difference between a lock and alatch?

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd