0% found this document useful (0 votes)
6 views2 pages

Problem1 DescriptionAndOutput

Uploaded by

pkm69782
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Problem1 DescriptionAndOutput

Uploaded by

pkm69782
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

DESCRIPTION OF PROBLEM 1:

This Java program is a minor project that focuses on rotating a given array of four integers to the right
by 2 bits. The program provides a simple command-line interface for user input and displays both the
original and resulting arrays in both integer and binary forms.

Here's a breakdown of the key components of the program:

Main Method:
 In the main method, the user is prompted to enter four integers, and the original array is
printed.
 The rotateRightBy2Bits method is then called to perform the rotation.
 Finally, the resulting array is printed.
User Input:
 The program uses a Scanner to take user input. It prompts the user to enter four integers
separated by spaces.
 The input values are stored in an integer array called array.
Array Rotation (rotateRightBy2Bits Method):
 The core logic of the program is in the rotateRightBy2Bits method.
 ‘temp’ is a temporary variable that stores the value of the last element of the array before any
modifications.
 For loop iterates over the array from the third element (i = 3) to the first element (i = 0).
 For each element at position i, it assigns the value of the preceding element (s[i - 1]) after
performing a bitwise rotation to the right.
 << 30 is a bitwise left shift by 30 positions, effectively moving the bits to the left by 30
positions.
 >>> 2 is a logical right shift by 2 positions, filling the leftmost bits with zeros.
 Finally, the first element of the array (s[0]) is assigned the value stored in the temp variable
after the same bitwise rotation process.
In summary, the method performs a right rotation of the array by 2 bits. It starts by saving the
original value of the last element, then iterates through the array, shifting each element to the
right by 2 bits, and finally assigns the saved value to the first element. The choice of bitwise
operations is to achieve the desired rotation effect.
Printing the Array (printArray Method):
 The printArray method is responsible for displaying both the integer and binary forms of the
array.
 It iterates over the array, printing each element in both decimal and binary representations.

Resource Management:
 The program uses a try-with-resources statement (try block with Scanner) to ensure proper
resource management, automatically closing the Scanner when done.

CONCLUSION:
Overall, this project is a basic implementation that focuses on fundamental concepts like user input,
array manipulation, bitwise operations, and output formatting.
OUTPUT 1:

Enter four integers separated by spaces:


10 11 12 13
Original Array:
Integer Form: 10 11 12 13
Binary Form: 1010 1011 1100 1101

Resulting Array:
Integer Form: 1073741826 -2147483646 -1073741821 3
Binary Form: 1000000000000000000000000000010
10000000000000000000000000000010 11000000000000000000000000000011 11

OUTPUT 2:
Enter four integers separated by spaces:
7 8 9 10
Original Array:
Integer Form: 7 8 9 10
Binary Form: 111 1000 1001 1010

Resulting Array:
Integer Form: -2147483647 -1073741822 2 1073741826
Binary Form: 10000000000000000000000000000001
11000000000000000000000000000010 10 1000000000000000000000000000010

You might also like