Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please solve this question: Write a Java function that performs the functionality of the headers below. String leftmost_one (int x) Namely, the leftmost_one method should

Please solve this question:

Write a Java function that performs the functionality of the headers below. String leftmost_one (int x)

Namely, the leftmost_one method should return a mask indicating the leftmost 1 in the input number x.

For example:

Input Output

0xFF00 0x8000

0x6600 0x4000

Moreover, you must implement this function using only straightline code (i.e., no loops or conditionals1 ) and a limited number of arithmetic and logical operators. Specifically, you are only allowed to use the following operators: , &, >>, >>, |,

Your code should contain a total of at most 15 arithmetic, bitwise, and logical operations. To return your solution as a String with hexadecimal format, you should use the in-built Java function:

String.format( 0x%08X, z),

where z is a int variable. For instance, System.out.println( String.format( 0x%08X, 65280) prints 0x0000FF00.

Hint: First transform x into a bit vector of the form [0 . . . 011111], where you turn any bit to right of the left most as 1. Then, think about how you can vanish all the 1s but the leftmost one.

Evaluation: This question is worth 18 marks. You will receive 3 marks (per function) simply for each of the following test cases:

Test case Input Input in hexadecimal Output

1 65,280 0x0000FF00 0x00008000

2 26,112 0x00006600 0x00004000

3 -680 0xFFFFFD58 0x80000000

4 0 0x00000000 0x00000000

5 2,147,483,647 0x7FFFFFFF 0x40000000

6 -2,147,483,648 0x80000000 0x8000000

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

Recommended Textbook for

Practical Oracle8I Building Efficient Databases

Authors: Jonathan Lewis

1st Edition

0201715848, 978-0201715842

More Books

Students also viewed these Databases questions