- 1
Reverse of a 32 bit unsigned integer given in a coding test(with hidden test cases)
You will be given variable list of unknown length, containing 32-bits unsigned integers. You are required to give as output a comma separated list, showing for each element in the list the integer you get by reversing bits in its binary representation. For example, if the list is 32768, 101200 and 262144, you are expected to give as output 65536, 181501952 and 8192. Integers in the list will be given one per line.
8 Answers
+ 6
Ashwin Goutham G Don't ask for code directly. Show your attempts first.
+ 2
Here's the working code in Java, this will Help!!
import java.util.*;
public class Convert{
public static void main(String []args){
Scanner sc=new Scanner(System.in);
int input;
String separator="";
while(sc.hasNextInt())
{
input=sc.nextInt();
System.out.print(separator+Integer.reverse(input));
separator=", ";
}
}
}
0
#include <stdio.h>
#include <math.h>
int main()
{
unsigned int i,n, in[10], z[10],rem, rev=0;
printf("Enter the value of n\n");
scanf("%d",&n);
printf("Enter the input values\n");
for(i=0;i<n;i++)
{
scanf("%u",&in[i]);
}
for(i=0;i<n;i++)
{
while(in[i]!=0)
{
rem=in[i] % 10;
rev=rev*10 +rem;
in[i]=in[i]/10;
z[i]=rev;
}
}
for(i=0;i<n;i++)
{
printf("%u",z[i]);
}
return(0);
}
- 1
This is the kind of result I want
Case 1:
For the input provided as follows:
32768
101200
262144
Output of the program will be
65536, 181501952, 8192
- 4
This is not the solution but just an attempt
- 4
I want to reverse the number's binary form but don't know how to do
- 4
This is the kind of result I want
Case 1:
For the input provided as follows:
32768
101200
262144
Output of the program will be
65536, 181501952, 8192