Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write C expressions that evaluate to 1 when the following conditions are true, a

ID: 3544410 • Letter: W

Question

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.

A. Any bit of x equals 1.

Any bit of x equals 0.

Any bit in the least significant byte of x equals 1.

D. Anybitinthemostsignificantbyteofxequals0.

Your code should follow the bit-level integer coding rules (page 120), with the additional restriction that you may not use equality (==) or inequality (!=) tests.


Write a function int_shifts_are_arithmetic() that yields 1 when run on a machine that uses arithmetic right shifts for ints, and 0 otherwise. Your code should work on a machine with any word size. Test your code on several machines.

Explanation / Answer

The first two cases are the simplest. You should think about boolean values in C : zero is false and non-zero is true.

Question C and D requires you to use the binary and operator and the solution of the first two questions.

And you don't need equality for any of these.

I'm not going to solve your exercise of course but as you see I gave you enough clue to solve it very easily.

Remember that (42 && 1) == 1. And remember what happens when you use twice the not operator (!!).

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote