Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1 Introduction The purpose of this assignment is to become more familiar with bit-level operations and bit-level representations of integers. Youll do this by solving

1 Introduction

The purpose of this assignment is to become more familiar with bit-level operations and bit-level representations of integers. Youll do this by solving a series of programming puzzles. Many of these puzzles are quite artificial, but youll find yourself thinking much more about bits in working your way through them.

2 Logistics This is an individual project. All handins are electronic. Clarifications and corrections will be posted to the discussion forum on D2L.

3 Handout Instructions When you login, you should see a directory named datalab in your home directory. It contains a number of files, but the only file you will be modifying and turning in is bits.c. The bits.c file contains a skeleton for each of the 11 programming puzzles. Your assignment is to complete each function skeleton using only straightline code (i.e., no loops or conditionals) and a limited number of C arithmetic and logical operators. Specifically, you are only allowed to use the following eight operators: ! & | + > A few of the functions further restrict this list. Also, you are not allowed to use any constants longer than 8 bits. See the comments in bits.c for detailed rules and a discussion of the desired coding style.

image text in transcribed

NOTE: None of the following codes have worked:

int greaterThan7(int x) {

int shiftedX = x >> 3; // shift x right by 3 to divide by 8

return !(shiftedX ^ 1); // use the XOR operator to compare shiftedX to 1, then use the NOT operator to return 1 if shiftedX is greater than 1 (i.e. x is greater than 7) and 0 if not

}

____________________________

int greaterThan7 (int x) {

return ((x - 7) >> 31) & 1;

}

___________________________

int greaterThan7(int x) {

int temp = x >> 31;

int check = (x + (~7 + 1)) >> 31;

return !(temp | check);

}

_______________________

int greaterThan7(int x) {

return !(x >> 31 & 1) & (x >> 3) & 1;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions