Generate and show all power-of-two numbers

Assignment Help Computer Engineering
Reference no: EM1330077

Write a Java program that uses bitwise operations to:

1. generate and display all power-of-two numbers in the range +128 to -128, and
2. display an arbitrary user-input integer.

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

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.

Your display(number) method should:
- Receive a number parameter.
- Print number's value and a tab.
- 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:
+ Use a conditional operator whose condition is (mask & number != 0) to print either 1 or 0.
+ After every fourth bit, print a single space to make the output readable.

Your program should mimic the sample session given below.

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

Reference no: EM1330077

Questions Cloud

Write down a program that ask the user to enter a word : Write down a program that ask the user for starting value and an ending value and then writes all the integers (inclusive) between those two values.
How do language and the question of a second language align : How do language and the question of a second language align with sending United States executives to overseas assignments?
Explain flexible benefits strategy : Prepare a benefits policy that encompasses the scope of benefits and a brief administrative section to address how benefits will be administered in your organization. Include a section on the roles necessary to carry out the policy within the orga..
Illustrtasome of the priceand how do expectations inluence : illustrtae what are some of the price and how do expectations inluence the effects of inflation.
Generate and show all power-of-two numbers : show an arbitrary user-input integer.generate and display all power-of-two numbers in the range +128 to -128.
Describe the health insurance benefits : Health Insurance Benefits and Human Resource Management - Show how an organization's compensation plan (specifically health insurance benefits) might impact an organization's strategic goal setting process.
Mass customization : How does mass customization help firms combat encroaching commoditization?
Explain business ethics : Explain Business Ethics and what environment factors would impact strategic decision making in large versus small multinational companies
Calculate the mortgage payments : A program written in Java (without a graphical user interface) that will compute and display the monthly payment amount to fully amortize a $200,000.00 loan over a 30 year term at 5.75? interest.

Reviews

Write a Review

Computer Engineering Questions & Answers

  Mathematics in computing

Binary search tree, and postorder and preorder traversal Determine the shortest path in Graph

  Ict governance

ICT is defined as the term of Information and communication technologies, it is diverse set of technical tools and resources used by the government agencies to communicate and produce, circulate, store, and manage all information.

  Implementation of memory management

Assignment covers the following eight topics and explore the implementation of memory management, processes and threads.

  Realize business and organizational data storage

Realize business and organizational data storage and fast access times are much more important than they have ever been. Compare and contrast magnetic tapes, magnetic disks, optical discs

  What is the protocol overhead

What are the advantages of using a compiled language over an interpreted one? Under what circumstances would you select to use an interpreted language?

  Implementation of memory management

Paper describes about memory management. How memory is used in executing programs and its critical support for applications.

  Define open and closed loop control systems

Define open and closed loop cotrol systems.Explain difference between time varying and time invariant control system wth suitable example.

  Prepare a proposal to deploy windows server

Prepare a proposal to deploy Windows Server onto an existing network based on the provided scenario.

  Security policy document project

Analyze security requirements and develop a security policy

  Write a procedure that produces independent stack objects

Write a procedure (make-stack) that produces independent stack objects, using a message-passing style, e.g.

  Define a suitable functional unit

Define a suitable functional unit for a comparative study between two different types of paint.

  Calculate yield to maturity and bond prices

Calculate yield to maturity (YTM) and bond prices

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd