Purchase Solution

Java program using bitwise operations

Not what you're looking for?

Ask Custom Question

Implement your solution using one class. In your class, provide two methods - main and display.

main method:

Your main method should:

· Declare int number = 128;
· Include a while loop that loops while number is >= -128.
· Call the display method, which prints the value of its passed-in number parameter.
· If number is greater than zero, use the >>= operator to do one arithmetic-right shift.
· If number equals zero, use the ~ operator to complement it.
· If number is less than zero, use the <<= operator to do one left shift.
· After the while loop, ask the user to input any number, and call the display method to print that number.

display(number) method:

Write the display(number) method like this:

· Receive a number parameter.
· Print number's value and a tab. (t is tab in java)
· Assign to a local variable named mask the value 1 shifted left 31 times. This puts a 1 in bit 31 and zeros in all other bits.
· Use a for loop to step through all 32 bits, doing the following in each step:
o Use a conditional operator whose condition is (mask & number != 0) to print either 1 or 0.
o After every fourth bit, print a single space to make the output readable.

Sample Session:

Decimal Binary

128 0000 0000 0000 0000 0000 0000 1000 0000
64 0000 0000 0000 0000 0000 0000 0100 0000
32 0000 0000 0000 0000 0000 0000 0010 0000
16 0000 0000 0000 0000 0000 0000 0001 0000
8 0000 0000 0000 0000 0000 0000 0000 1000
4 0000 0000 0000 0000 0000 0000 0000 0100
2 0000 0000 0000 0000 0000 0000 0000 0010
1 0000 0000 0000 0000 0000 0000 0000 0001
0 0000 0000 0000 0000 0000 0000 0000 0000
-1 1111 1111 1111 1111 1111 1111 1111 1111
-2 1111 1111 1111 1111 1111 1111 1111 1110
-4 1111 1111 1111 1111 1111 1111 1111 1100
-8 1111 1111 1111 1111 1111 1111 1111 1000
-16 1111 1111 1111 1111 1111 1111 1111 0000
-32 1111 1111 1111 1111 1111 1111 1110 0000
-64 1111 1111 1111 1111 1111 1111 1100 0000
-128 1111 1111 1111 1111 1111 1111 1000 0000

Enter any integer: 127

127 0000 0000 0000 0000 0000 0000 0111 1111

Attached is what I have come up with but am making a mistake somewhere because I can't get it to compile and run.
Please help.

Purchase this Solution

Solution Summary

Solutions merely fixes the slip in the submitted version of Java code.

Solution Preview

Please rename the attached 280213.java as YourNameProg3.java before you compile ...

Purchase this Solution


Free BrainMass Quizzes
Basic UNIX commands

Use this quiz to check your knowledge of a few common UNIX commands. The quiz covers some of the most essential UNIX commands and their basic usage. If you can pass this quiz then you are clearly on your way to becoming an effective UNIX command line user.

Java loops

This quiz checks your knowledge of for and while loops in Java. For and while loops are essential building blocks for all Java programs. Having a solid understanding of these constructs is critical for success in programming Java.

Basic Computer Terms

We use many basic terms like bit, pixel in our usual conversations about computers. Are we aware of what these mean? This little quiz is an attempt towards discovering that.

Basic Networking Questions

This quiz consists of some basic networking questions.

Inserting and deleting in a linked list

This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.