int getBit(int bn, unsigned long val): extracts a single bit value at position on from the...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
● ● int getBit(int bn, unsigned long val): extracts a single bit value at position on from the target variable val, and returns an integer 0 or 1 depending on the value of the bn'th bit in val. void setBit(int bn, int new, unsigned long *val) : Sets the value of the bn bit in the target variable val to the value in new. The bn'th bit in val is set to 1 if new is non-zero, or 0 if new is zero. long getBit Fld (int bs,int len, unsigned long val): extracts a subfield starting at position bs for a length of len from the target variable val and returns a signed long integer that represents the result treated as a signed two's complement value. • unsigned long getBitFldU(int bs, int len, unsigned long val): extracts a subfield starting at position bs for a length of len from the target variable val and returns an unsigned long integer that represents the result treated as an unsigned binary number. void setBitFld (int bs, int len, unsigned long new, unsigned long *val): inserts the value represented by the rightmost len bits of new (which may be signed or unsigned) into the unsigned long target pointed to by val starting at position bs for length len . #include "bitFields.h" #include <assert.h> int getBit(int bn, unsigned long val) { assert (bn>= 0 && bn<64); if (1UL<<bn & val) return 1; return 0; } void setBit(int bn, int new, unsigned long *val) { assert (bn>= 0 && bn<64); if (new) (*val) |=1UL<<bn; else (*val)&=~(1L<<bn); } long getBitFld (int bs, int len, unsigned long val) { // TODO: // // // // // } unsigned long getBitFldU(int bs, int len, unsigned long val) { // TODO: // // // // // // // // } check to make sure bs is a valid bit position in long (you may assert this is true) check to make sure len is a valid length for a subfield that starts at bs (you may assert this is true) perform an unsigned getBitFldU to extract the unsigned version of the field get the high order bit of the result... that's the "sign" bit if the sign bit not zero, replace the padding with 1 padding return 0; // replace this with the correct code } check to make sure bs is a valid bit position in long (you may assert this is true) check to make sure len is a valid length for a subfield that starts at bs (you may assert this is true) make a mask that has len 1's. Shift that mask left so it's leftmost 1 starts at bs bitwise and the mask and val to isolate the bit field bits Shift the result to the right so that the rightmost bit of the bit field is bit 0 Note that shift right pads to the left with the sign bit if the field is signed, but for unsigned, it will pad to the left with 0 return 0; // replace this with the correct code void setBit Fld (int bs, int len, unsigned long new, unsigned long *val) { // TODO: // // // // // // check to make sure bs is a valid bit position in long (you may assert this is true) check to make sure len is a valid length for a subfield that starts at bs (you may assert this is true) Make a mask that has len bits, right justified Use mask to turn off any bits to the left of the bitField values in new Shift both the new value and the mask to the correct bit position Turn off all bits in *val under the mask by bitwise anding with mask Or *val with new. (new has zeros for all non bit field positions, so won't affect val) ● ● int getBit(int bn, unsigned long val): extracts a single bit value at position on from the target variable val, and returns an integer 0 or 1 depending on the value of the bn'th bit in val. void setBit(int bn, int new, unsigned long *val) : Sets the value of the bn bit in the target variable val to the value in new. The bn'th bit in val is set to 1 if new is non-zero, or 0 if new is zero. long getBit Fld (int bs,int len, unsigned long val): extracts a subfield starting at position bs for a length of len from the target variable val and returns a signed long integer that represents the result treated as a signed two's complement value. • unsigned long getBitFldU(int bs, int len, unsigned long val): extracts a subfield starting at position bs for a length of len from the target variable val and returns an unsigned long integer that represents the result treated as an unsigned binary number. void setBitFld (int bs, int len, unsigned long new, unsigned long *val): inserts the value represented by the rightmost len bits of new (which may be signed or unsigned) into the unsigned long target pointed to by val starting at position bs for length len . #include "bitFields.h" #include <assert.h> int getBit(int bn, unsigned long val) { assert (bn>= 0 && bn<64); if (1UL<<bn & val) return 1; return 0; } void setBit(int bn, int new, unsigned long *val) { assert (bn>= 0 && bn<64); if (new) (*val) |=1UL<<bn; else (*val)&=~(1L<<bn); } long getBitFld (int bs, int len, unsigned long val) { // TODO: // // // // // } unsigned long getBitFldU(int bs, int len, unsigned long val) { // TODO: // // // // // // // // } check to make sure bs is a valid bit position in long (you may assert this is true) check to make sure len is a valid length for a subfield that starts at bs (you may assert this is true) perform an unsigned getBitFldU to extract the unsigned version of the field get the high order bit of the result... that's the "sign" bit if the sign bit not zero, replace the padding with 1 padding return 0; // replace this with the correct code } check to make sure bs is a valid bit position in long (you may assert this is true) check to make sure len is a valid length for a subfield that starts at bs (you may assert this is true) make a mask that has len 1's. Shift that mask left so it's leftmost 1 starts at bs bitwise and the mask and val to isolate the bit field bits Shift the result to the right so that the rightmost bit of the bit field is bit 0 Note that shift right pads to the left with the sign bit if the field is signed, but for unsigned, it will pad to the left with 0 return 0; // replace this with the correct code void setBit Fld (int bs, int len, unsigned long new, unsigned long *val) { // TODO: // // // // // // check to make sure bs is a valid bit position in long (you may assert this is true) check to make sure len is a valid length for a subfield that starts at bs (you may assert this is true) Make a mask that has len bits, right justified Use mask to turn off any bits to the left of the bitField values in new Shift both the new value and the mask to the correct bit position Turn off all bits in *val under the mask by bitwise anding with mask Or *val with new. (new has zeros for all non bit field positions, so won't affect val)
Expert Answer:
Answer rating: 100% (QA)
It seems that you have provided images that contain descriptions and implementations of bit ... View the full answer
Related Book For
Posted Date:
Students also viewed these programming questions
-
aub =0.08. The area and chord of the wing are 0.1m and 0.1 m, respectively. Now assume that a horizontal tail is added to this model. We have the flowing parameters: =0.17m, S = 0.02m, i, = 2.5,...
-
Choose the correct option. XYZ Co. reported the following cost of goods over the last 3 years: $1M in year 1; $1.2M in year 2; $1.25M in year 3. It also reported the following revenue over the last 3...
-
Example 4.35 and Figure 4.56 show how a circuit that generates an ASCII byte suitable for sending over a communications link may be defined. Write Verilog code for its counterpart at the receiving...
-
What types of decisions are made by football managers? Would you characterize these decisions as structured or unstructured problems? Explain.
-
A manometer is used to measure the air pressure in a tank. The fluid used has a specific gravity of 1.25, and the differential height between the two arms of the manometer is 28 in. If the local...
-
a. Referring to Fig. 1.33, determine the transition capacitance at reverse-bias potentials of -25 V and -10 V. What is the ratio of the change in capacitance to the change in voltage? b. Repeat part...
-
The Wide World of Fluids article titled "Grand Canyon Rapids Building.". During the flood of 1983, a large hydraulic jump formed at "Crystal Rapid" on the Colorado River. People rafting the river at...
-
Using net present value and internal rate of return to evaluate investment opportunities Kerry Woods rich uncle gave him $60,000 cash as a gift for his 40th birthday. Unlike his spoiled cousins who...
-
What does the Performance Management (PM) process consist of? On what motivational theory is the PM process based on?
-
San Francisco led the movement to ban plastic shopping bags and is now proposing to ensure people have access to safe high-quality tap water and ban bottled water. A supporter of the ban says The...
-
1. Examine in depth the challenges Apple faces in managing offshore and outsourced operations especially in terms of how these challenges affect corporate social responsibility (CSR) outcomes. 2....
-
As of the late 1990's (post KM's death), how well has corporate management succeeded in propagating KM's approach to innovation beyond the Japanese culture? Give specific examples of successes and/or...
-
Prepare the journal entries for the following transactions: Clearly indicate what is a DR and what is a CR. When identifying the account, put in brackets an (A) for an asset, (L) for liability, and...
-
An Insurance company that insures cars figured the distribution of individual claims for year 2008 as follows: Pr[X=2000]=2*Pr[X=4000]=3*Pr[X=6000] . The company has a supreme cover limit in claims...
-
BS Ltd provides consultancy services to small and medium sized businesses. Three types of consultants are employed offering administrative, data processing and marketing advice respectively. The...
-
(a)What drives the development of transportation routes and technology? (b)What economic or social development is driven by transportation developments?
-
The financial statements of Ayayai Ltd. include the following items at December 31, 2025. Accounts payable Accounts receivable Accumulated depreciation-buildings Accumulated depreciation-equipment...
-
X-1 Find the domain of the function f(x) : x 1 2 - O (-00, -1) U (-1, ) O (-00, 1) U (1, ) O -00, -1) U (-1, 1) U (1, 0) O (- 1, 1)
-
Modify the grammar of Figure 2.25 so that it accepts only programs that contain at least one write statement. Make the same change in the solution to Exercise 2.17. Based on your experience, what do...
-
Bjarne Stroustrup, the original designer of C++, once described templates as a clever kind of macro that obeys the scope, naming, and type rules of C++ [Str13, 2nd ed., p. 257]. How close is the...
-
Use lazy evaluation (delay and force) to implement iterator objects in Scheme. More specifically, let an iterator be either the null list or a pair consisting of an element and a promise which when...
-
A food processor claims that at most \(10 \%\) of her jars of instant coffee contain less coffee than claimed on the label. To test this claim, 16 jars of her instant coffee are randomly selected and...
-
Refer to Exercise 4.2. (a) Determine the cumulative probability distribution \(F(x)\). (b) Graph the probability distribution of \(f(x)\) as a bar chart and below it graph \(F(x)\). Data From...
-
Four emergency radios are available for rescue workers but one does not work properly. Two randomly selected radios are taken on a rescue mission. Let \(X\) be the number that work properly between...
Study smarter with the SolutionInn App