What is the output of the scenario?
Question
What is the output of the scenario?
1. I have an integer array [123, 5496, 2387, 63287, 1, 4000, 7658].
2. I have a 32-byte memory.
3. I want to save the array to the memory starting from address 2.
4. Once the array is saved, I get all the binary data from the memory addresses that are divisible by 4.
5. I transformed each binary data that I got to a decimal value.
6. After I transformed all binary data to a decimal value, I added all decimal values to get a total.
7. I displayed the total.
Summary
In this question, we have been provided an integer array, and an array is containing seven elements. Among all the elements in the array, the maximum number occupies 16 bits. We will save this array to a memory address starting from the 2. Here, some steps are given and we have to tell the output for this question. Hence, the value that is stored in the bytes which is a multiple of four, will be obtained by just diving the number with 255. The output of this program will be 321.
Explanation
123 in binary: 0111 1011
5496 in binary: 0001 0101 0111 1000
2387 in binary: 0000 1001 0101 0011
63287 in binary: 1111 0111 0011 0111
1 in binary: 0001
4000 in binary: 0000 1111 1010 0000
7658 in binary: 0001 1101 1110 1010
Location | Data |
2 | 0000 0000 |
3 | 0000 0000 |
4 | 0000 0000 |
5 | 0111 1011 |
6 | 0000 0000 |
7 | 0000 0000 |
8 | 0001 0101 |
9 | 0111 1000 |
10 | 0000 0000 |
11 | 0000 0000 |
12 | 0000 1001 |
20 | 0000 0000 |
21 | 0000 0001 |
22 | 0000 0000 |
23 | 0000 0000 |
24 | 0000 1111 |
25 | 1010 0000 |
26 | 0000 0000 |
27 | 0000 0000 |
28 | 0001 1101 |
29 | 1110 1010 |
Locations that are multiples of 4:
4, 8, 12, 16, 20, 24, 28
4 -> 0000 0000 = 0
8 -> 0001 0101 = 21
12 -> 0000 1001 = 9
16 -> 1111 0111 = 247
20 -> 0000 0000 = 0
24 -> 0000 1111 = 15
28 -> 0001 1101 = 29
The sum of these values = (0 + 21 + 9 + 247 + 0+ 15 + 29) = 321
Code
import java.util.*; class Main{ public static void main(String args[]){ int arr[]={123,5496,2387,63287,1,4000,7658}; int start[]={2,6,10,14,18,22,26}; int end[]={5,9,13,17,21,25,29}; int sum=0; for(int i=0;i<7;i++){ for(int j=start[i];j<=end[i];j++){ if(j%4==0){ int x=arr[i]/256; sum=sum+x; } } } System.out.println(sum); } }
Output of the scenario
Also, read How do I convert a String to an int in Java?