188k views
5 votes
Write C expressions that evaluate to 1 when the following conditions are true and to 0 when they are false. Assume x is of type int. Any bit of x equals 1. Any bit of x equals 0. Any bit in the least significant byte of x equals 1. Any bit in the most significant byte of x equals 0. Your code should follow the bit-level integer coding rules (page 128), with the additional restriction that you may not use equality (==) or inequality (!=) tests.

1 Answer

3 votes

Answer:

Check the explanation

Step-by-step explanation:

#include <stdio.h>

#include <stdlib.h>

//function to print a message

static void printMessage(char *msg)

{

printf("%s\\", msg);

}

//perform option A

int perform_A(int x)

{

//return true if any bit of x is equal to 1

return !!x;

}

//perform option A

int perform_B(int x)

{

//return true if any bit of x is equal to 0

return !x;

}

//perform option C

int perform_C(int x)

{

//return true if least significant bit is 1

return !!(x & 0xFF);

}

//perform option D

int perform_D(int x)

{

//return true if most significant bit is 0

return !!(~x & 0xFF);

}

//the main function

int main(void)

{

//set the integer

int input = sizeof(int) << 3;

//declare the test values

int onebitis1 = ~0;

int onebitis0 = 0;

int onebitLeast1 = 0xFF;

int onebitMost0 = 0xFF << (input - 8);

(perform_A(onebitis1)) && (printf("A return True\\"));

(perform_B(onebitis0)) && (printf("B returns True\12"));

(perform_C(onebitLeast1)) && (printf("C returns True\\"));

(perform_D(onebitMost0)) && (printf("D returns True\\"));

//to wait for a key press

getchar();

return 0;

}

Kindly check the attached image below to see the output.

Write C expressions that evaluate to 1 when the following conditions are true and-example-1
User HorHAY
by
5.5k points