Write a program that asks the user for an integer value (may be negative) and th
ID: 3786132 • Letter: W
Question
Write a program that asks the user for an integer value (may be negative) and that outputs the
bit pattern for that integer, starting with the most significant bit. You should use an int data
type which, on nearly all modern compilers, is a 32-bit value. Your output should print the
decimal value, the 8-digit hexadecimal representation, and the 32-bit binary bit pattern with
the bits in groups of four separated by a space.
You are required to write (and use) the function-like macro
testbit(integer, position)
that evaluates to true (1) or false (0) depending on if the bit in the indicated position (with 0
being the least significant bit) is a 1 or 0, respectively.
Run your program for a selection of small, moderate, and large integer values, both positive
and negative, placing the results in a ReadMe.txt file.
NOTE: DO NOT to use any library functions that perform the significant tasks, such as rounding a value
to the nearest cent.
Explanation / Answer
Answer:
import java.util.*;
public class Bitpatternprogram
{
public static void main(String[] args)
{
int read, value;
String input = "";
Scanner s = new Scanner(System.in);
System.out.print("Enter any number which is integer:");
read = s.nextInt();
while(read > 0)
{
value = read % 2;
input = input + "" + value;
read = read / 2;
}
System.out.println("Bit pattern for the integer number is :"+input);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.