A.
Explanation of Solution
C expression for given statement:
The C expression for any bit of “x” equals “1” is shown below.
Expression: !~x
For the above expression “!~x”, a complete program has been developed in the below section:
//Header file
#include <stdio.h>
#include <assert.h>
//Function definition "A" statement
int partA(int x)
{
/* Returns "1" if any bits of "x" equals "1" by using expression "!~x" */
return !~x;
}
//Main function
int main(int argc, char* argv[])
{
//Assign all bit is "1"
int allBit1 = ~0;
//Assign all bit is "0"
int allBit0 = 0;
/* Call partA function with "alllBit1" using "assert" function */
assert(partA(allBit1));
/* Call partA function with "alllBit0" using "assert" function */
assert(!partA(allBit0));
/* Call partA function with number "0x1234FF" using "assert" function */
assert(!partA(0x1234FF));
return 0;
}
From the given code,
- Define the function for “partA()”
- In this function, returns “1” if any bits of “x” equals “1” by using the expression “!~x”...
B.
Explanation of Solution
C expression for given statement:
The C expression for any bit of “x” equals “0” is shown below.
Expression: !x
Program:
For the above expression “!x”, a complete program has been developed in the below section:
//Header file
#include <stdio.h>
#include <assert.h>
//Function definition "B" statement
int partB(int x)
{
/* Returns "1" if any bits of "x" equals "0" by using expression "!x" */
return !x;
}
//Main function
int main(int argc, char* argv[])
{
//Assign all bit is "1"
int allBit1 = ~0;
//Assign all bit is "0"
int allBit0 = 0;
/* Call partB function with "alllBit1" using "assert" function */
assert(!partB(allBit1));
/* Call partB function with "alllBit0" using "assert" function */
assert(partB(allBit0));
/* Call partB function with number "0x1234FF" using "assert" function */
assert(!partB(0x1234FF));
return 0;
}
From the given code,
- Define the function for “partB()”
- In this function, returns “1” if any bits of “x” equals “0” by using the expression “!x”.
- Define the main function
- In this function, assign “~0” in variable “allBit1”...
C.
Explanation of Solution
C expression for given statement:
The C expression for any bit in the least significant byte of “x” equals “1” is shown below.
Expression: !~(x | ~0xff)
Program:
For the above expression “!~(x | ~0xff)”, a complete program has been developed in the below section:
//Header file
#include <stdio.h>
#include <assert.h>
//Function definition "A" statement
int partA(int x)
{
/* Returns "1" if any bits of "x" equals "1" by using expression "!~x" */
return !~x;
}
//Function definition "C" statement
int partC(int x)
{
/* Returns "1" if any bits in the least significant byte of "x" equals "1" by calling the function "partA" with "x | ~0xff" */
return partA(x | ~0xff);
}
//Main function
int main(int argc, char* argv[])
{
//Assign all bit is "1"
int allBit1 = ~0;
//Assign all bit is "0"
int allBit0 = 0;
/* Call partC function with "alllBit1" using "assert" function */
assert(partC(allBit1));
/* Call partC function with "alllBit0" using "assert" function */
assert(!partC(allBit0));
/* Call partC function with number "0x1234FF" using "assert" function */
assert(partC(0x1234ff));
return 0;
}
From the given code,
- Define the function for “partA()”
- In this function, returns “1” if any bits of “x” equals “1” by using the expression “!~x”...
D.
Explanation of Solution
C expression for given statement:
The C expression for any bit in the least significant byte of “x” equals “0” is shown below.
Expression: !((x >> ((sizeof(int)-1) << 3)) & 0xff)
Program:
For the above expression “!((x >> ((sizeof(int)-1) << 3)) & 0xff)”, a complete program has been developed in the below section:
//Header file
#include <stdio.h>
#include <assert.h>
//Function definition "B" statement
int partB(int x)
{
/* Returns "1" if any bits of "x" equals "0" by using expression "!x" */
return !x;
}
//Function definition "D" statement
int partD(int x)
{
/* Returns "1" if any bits in the least significant byte of "x" equals "0" by calling the function "partB" with "x >> ((sizeof(int)-1) << 3)) & 0xff" */
return partB((x >> ((sizeof(int)-1) << 3)) & 0xff);
}
//Main function
int main(int argc, char* argv[])
{
//Assign all bit is "1"
int allBit1 = ~0;
//Assign all bit is "0"
int allBit0 = 0;
/* Call partB function with "alllBit1" using "assert" function */
assert(!partD(allBit1));
/* Call partB function with "alllBit0" using "assert" function */
assert(partD(allBit0));
/* Call partB function with number "0x1234FF" using "assert" function */
assert(partD(0x1234ff));
return 0;
}
From the given code,
- Define the function for “partB()”
- In this function, returns “1” if any bits of “x” equals “0” by using the expression “!x”...
Want to see the full answer?
Check out a sample textbook solutionChapter 2 Solutions
Computer Systems: A Programmer's Perspective (3rd Edition)
- Write a C Program to shift bits. You will take input from the command-line argument (i.e argv[1]. Input should be 8 bits long in binary (i.e 01010011). Shifting means exactly that, shift all the bits in the first operand either left or right the number of time indicated in the second. The second operand should be an integer value. If it is not, then truncate it (pretend the fractional part does not exist). If a shift left would push a bit off of the 'front', it is lost and causes an overflow. Report the overflow and print the bits left over after the shift. If a shift right would push a bit off of the 'back', it is lost and causes an underflow. Report the underflow and print the bits left over after the shift.e.g.(shift 2 to the left 1 time .. in essence multiply by 2)$ ./compu "000101000010"$ OKAY 1000 (shift 2 to the left 1.5 times .. not an int)$ ./compu "000101000011"$ NINT 1000arrow_forwardWe are given an unsigned integer number represented by 16 bits. This number can be kept in a variable defined in C language as "uint16_t". Let's represent this variabe as "uint16_t x;" in our C code. a) keep the least significant 8 bits of this number in an unsigned character variable "x_low". b) Keep the most significant 8 bits of this number in an unsigned character variable "x_high". c) Form a new unsigned integer 16-bit variable in C language called "x_swap". This variable will keep the swapped version of the variable "x" such that the least significant 8 bits of the variable x will become the most significant 8 bits of the variable "x_swap". Also, the most significant 8 bits of the variable x will become the least significant 8 bits of the variable "x_swap".arrow_forwardWrite a simple program that prints the results of all the operators available in C (including pre/ post increment , bitwise and/or/not , etc.). Read required operand values from standard input.Expected Output:Enter a,b values:20 10 Arithmetic operators result is a+b->30 a-b->10 a*b->200 a/b->2 a%b->0 Relational operators result is a>b->1 a<b->0 a>=b->1 a<=b->0 a==b->0 a!=b->1 Logical operators result is a&&b->1 a||b->1 !(a==b)->1 Minimum of given values using Ternary Operator is ->10 Assignment Operator result a is ->10 Unary operator result is 11 11 9 9 Bitwise operators result is a&b->8 a|b->12 a^b->4 a<<2->48 b>>3->1 Size of a is 4 While using unary increment operators the unary output is 10 12 10 8 and not 11 11 9 9arrow_forward
- // Task 3 // For this function, you must return the largest power of 2 that // is less than or equal to x (which will be positive). You may // not use multiplication or some sort of power function to do this, // and should instead rely on bitwise operations and the underlying // binary representation of x. If x is 0, then you should return 0. unsigned largest_po2_le(unsigned x) { return x; }arrow_forwardWhat will be the weakest precondition for the following lines of code a) if (x-y) > 0 then z = y; else z = x; {z <= y ^ z <= x} b) a = 1; y = x; y = y - a (y > 0 ^ x > y)arrow_forwardWrite a C-function with two arguments (n and r) that has prototype: char clearbit(char k, char bits) The function clears (sets to 0) the bit number k (in the range of 0 to 7) in bits and returns the resulting value. For example, if k is 0x02 and bits is 0x07, the function would return bits with its k’th bit cleared, resulting in 0x03. It must not change other bits in bits. Hint: You may use any number of C-statements, but this task can be accomplished in as few as one!arrow_forward
- In c++ Now write a program for a double floating pointtype.•What are the number of significant bits that the double’s mantissa can hold, excluding the sign bit.•What is the largest number that a double’s mantissa can hold without roundoff error?•Repeat the program shown on the previous page but set to show where the double’s mantissa starts to exhibit roundoff errors.arrow_forward(ix) Write a C++ program read input an integer i in the range [1,8] and find whether the ith bit of the number 2000 is 0 or 1.arrow_forwardf(x)=x^2(x-4)(x+2)^2 Bounce(B) or Cross(C) (in ascending order of zeros): Incorrect (So type CBC or BBB or...)arrow_forward
- BELOW MCQ GIVEN ANSWER CAN BE MORE THAN ONE OPTION. PLEASE PROVIDE CORRECT ANSWERS. ------------------- Q3 :- The ANSI C function below causes the program in which it runs to malfunction . Which of the following connection will help to perform function successfully ? /* Return a count of all the bits set in bytes */ int bitcount (unsigned char c) { unsigned char x , b , count =0; for (b = 0 , x= 1 ; !(x & 0x100); ++b, x <<=1) { if (x | c ) ++count; } return count; } A) change the type of x to a type larger than unsigned char. B) remove the extraneous variable b. C) change the return type to unsigned char . D) change if ( x | c) to if ( x & c) inside the for loop. E) change ! (c & 0x100) to ( x < 0x100) inside the for loop.arrow_forwardWrite a C++ program for the conversion of a decimal number to a binary, octal and hexadecimalequivalents within a given range of the upper and lower limits. Your program needs to follow thefollowing steps to give the desired output.(1.a). Your program should ask to choose the upper limit from 256, 128, 32, 16, 8, 4, 2 only. If auser enters a number other than these numbers, the program should display a message of wrongentry and should ask for retry, otherwise continue to step (b).(1.b). Your program should ask to enter the lower limit. The lower limit should be less than theupper limit (of step 1.a) and greater than 0. If a user enters a number greater than the upper limitor less than 0 (e.g., a negative value), then the program should display a message of wrong entryand should ask for retry, otherwise continue to display the output.(1.c). If a user enters the upper and lower limits correctly, then the program should print a table ofthe binary, octal and hexadecimal equivalents of the…arrow_forwardConsider a code designed to specify one of four possible directions: up:000 down:110 left:011 right:101 can this code detect any single error made during the transmission of a codeword? can it correct the error?arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education