Question

Create a program (java): that reads a 8-bit String input, must be read in as such....

Create a program (java): that reads a 8-bit String input, must be read in as such. Then convert that string of 1s and 0s to decimal as if it were encoded using the following representations:

Unsigned

1's Complement

2's Complement

Q(4.4) (2's complement)

1 bit (sign) - 3 bits (exponent: use bias) - 4 bits (significand), implied binary point w/ 1. Also check for 0....

Do not use helper functions, must be done iterating string and modifying an int/long value.

Desired execution:

Enter a binary number: 10011010
Unsigned: 154
1's Comp: -101
2's Comp: -102
Q(4.4): -6.375
S-Exp-Sig: -0.40625
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;
class Main{
public static long Power(int base,int exp){
long res=1;
for(int i=0;i<exp;i++){
res=res*base;
}
return res;
}
public static long Bin2Dec(String bin){
long dec=0,sum=0;
int n = bin.length(),j=0;
for(int i=n-1;i>=0;i--){
if(bin.charAt(i)=='0'){
dec=0;
}
else if(bin.charAt(i)=='1'){
dec=1*Power(2,j);
}
j++;
sum=sum+dec;
}
return sum;
}
  
public static long Unsigned(String bin){
bin='0'+bin;
long dec=0;
int n = bin.length();
for(int i=0;i<n;i++){
if(bin.charAt(i)=='0'){
bin.replace('0', '1');
}
else if(bin.charAt(i)=='1'){
bin.replace('1', '0');
}
}
dec=Bin2Dec(bin);
return dec;
}
  
public static long OnesComp(String bin){
long dec=0;
int n = bin.length();
String nwbin;
long sym=1;
StringBuilder builder = new StringBuilder(bin);
if (bin.charAt(0) == '0') {
sym=1;
}
else if (bin.charAt(0) == '1') {
sym=-1;
}
for (int i = 0; i < n; i++) {
if (bin.charAt(i) == '0') {
builder.setCharAt(i, '1');
}
else if (bin.charAt(i) == '1') {
builder.setCharAt(i, '0');
}
}
nwbin = builder.toString();
dec=Bin2Dec(nwbin);
dec=dec*sym;
return dec;
}
public static long TwosComp(String bin){
long dec;
if(OnesComp(bin)<0){
dec=OnesComp(bin)-1;
}
else{
dec=OnesComp(bin)+1;
}
return dec;
}
  
/*
public static long Sig_Exp(String bin){
long sym;
if (bin.charAt(i) == '0') {
sym=1;
}
else if (bin.charAt(i) == '1') {
sym=-1;
}
}
*/
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.print("Enter a Number : ");
String i=sc.nextLine();
System.out.println("Unsigned of "+i+" : "+Unsigned(i));
System.out.println("One's Complement of "+i+" : "+OnesComp(i));
System.out.println("Two's Complement of "+i+" : "+TwosComp(i));
}
}

Add a comment
Know the answer?
Add Answer to:
Create a program (java): that reads a 8-bit String input, must be read in as such....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • If we use the IEEE standard floating-point single-precision representation (1 sign bit, 8 bit exponent bits...

    If we use the IEEE standard floating-point single-precision representation (1 sign bit, 8 bit exponent bits using excess-127 representation, 23 significand bits with implied bit), then which of the following hexadecimal number is equal to the decimal value 3.875? C0780000 40007800 Oo 40780000 40A80010 The binary string 01001001110000 is a floating-point number expressed using a simplified 14-bit floating-point representation format (1 sign bit, 5 exponent bits using excess-15 representation, and 8 significand bits with no implied bit). What is its...

  • (3 pts) Consider an unsigned fixed point decimal (Base10) representation with 8 digits, 5 to the...

    (3 pts) Consider an unsigned fixed point decimal (Base10) representation with 8 digits, 5 to the left of the decimal point and 3 to the right. a.      What is the range of the expressible numbers?    b.      What is the precision?    c.       What is the error?    ______________________________________________________________________________   (3 pts) Convert this unsigned base 2 number, 1001 10112, to each base given below   (Note: the space in the binary string is purely for visual convenience) Show your work. Using...

  • I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I...

    I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I just have to explain a lot so you understand how the program should work. In C programming, write a simple program to take a text file as input and encrypt/decrypt it by reading the text bit by bit, and swap the bits if it is specified by the first line of the text file to do so (will explain below, and please let me...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT