import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
String m = "";
while ((m = br.readLine()) != null) {
StringBuilder temp=null ;
if(m.charAt(0)=='-'){
String str=m.substring(1);
str= Long.toBinaryString(Long.parseLong(str));
temp = new StringBuilder(twoSComplement(str));
}else{
temp = new StringBuilder(Long.toString(Integer.parseInt(m), 2));
}
sb.append(m).append(" converts to ").append(operation(temp)).append("\n");
}
System.out.print(sb);
}
static String operation(StringBuilder temp) {
while (temp.length() < 32) {
temp.insert(0, "0");
}
StringBuilder ans = new StringBuilder();
for (int i = 0; i < 8; i++) {
ans.append(temp.charAt(i + 24));
}
for (int i = 0; i < 8; i++) {
ans.append(temp.charAt(i + 16));
}
for (int i = 0; i < 8; i++) {
ans.append(temp.charAt(i + 8));
}
for (int i = 0; i < 8; i++) {
ans.append(temp.charAt(i));
}
return StringMan(ans);
}
static String StringMan(StringBuilder temp) {
if (temp.charAt(0) == '1') {
StringBuilder oneComplement = new StringBuilder();
for (int i = 0; i < temp.length(); i++) {
if (temp.charAt(i) == '0') {
oneComplement.append('1');
} else {
oneComplement.append('0');
}
}
long h=Long.parseLong(oneComplement.toString(),2)+1;
return "-"+h;
}
return Integer.parseInt(temp.toString(), 2) + "";
}
static String twoSComplement(String temp){
StringBuilder oneComplement = new StringBuilder();
while(temp.length()<32){
temp='0'+temp;
}
for (int i = 0; i < temp.length(); i++) {
if (temp.charAt(i) == '0') {
oneComplement.append('1');
} else {
oneComplement.append('0');
}
}
long h=Long.parseLong(oneComplement.toString(),2)+1;
return Long.toBinaryString(h);
}
}
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
String m = "";
while ((m = br.readLine()) != null) {
StringBuilder temp=null ;
if(m.charAt(0)=='-'){
String str=m.substring(1);
str= Long.toBinaryString(Long.parseLong(str));
temp = new StringBuilder(twoSComplement(str));
}else{
temp = new StringBuilder(Long.toString(Integer.parseInt(m), 2));
}
sb.append(m).append(" converts to ").append(operation(temp)).append("\n");
}
System.out.print(sb);
}
static String operation(StringBuilder temp) {
while (temp.length() < 32) {
temp.insert(0, "0");
}
StringBuilder ans = new StringBuilder();
for (int i = 0; i < 8; i++) {
ans.append(temp.charAt(i + 24));
}
for (int i = 0; i < 8; i++) {
ans.append(temp.charAt(i + 16));
}
for (int i = 0; i < 8; i++) {
ans.append(temp.charAt(i + 8));
}
for (int i = 0; i < 8; i++) {
ans.append(temp.charAt(i));
}
return StringMan(ans);
}
static String StringMan(StringBuilder temp) {
if (temp.charAt(0) == '1') {
StringBuilder oneComplement = new StringBuilder();
for (int i = 0; i < temp.length(); i++) {
if (temp.charAt(i) == '0') {
oneComplement.append('1');
} else {
oneComplement.append('0');
}
}
long h=Long.parseLong(oneComplement.toString(),2)+1;
return "-"+h;
}
return Integer.parseInt(temp.toString(), 2) + "";
}
static String twoSComplement(String temp){
StringBuilder oneComplement = new StringBuilder();
while(temp.length()<32){
temp='0'+temp;
}
for (int i = 0; i < temp.length(); i++) {
if (temp.charAt(i) == '0') {
oneComplement.append('1');
} else {
oneComplement.append('0');
}
}
long h=Long.parseLong(oneComplement.toString(),2)+1;
return Long.toBinaryString(h);
}
}
No comments:
Post a Comment