8 Bit Float To Decimal

Problem:

Preparing for another Exam, I hit the issue of finding an online converter to use with an 8 bit floating number to decimal and the other way around. A round of searching did not bring up anything that would resolve that problem. Figured it would be an interesting program to make. First up is the easy part, converting a string of zeros and ones into a decimal number by following the rules according to the school textbook.

8bitFloatExample

In this example we have mantissa that includes “1011″ and exponent “110″.
The exponent indicates how many steps we have to move the comma. The larger the exponent, the larger the final number will be.
This describes the value of the exponent. (It’s 3 bit eksess-n)
OffsetBinary3bit
So that gives exponent a value of two. That means we move the mantissa two steps to the left, or as I prefer to think of it, we enlarge the mantissa by two steps. So “0.1011″ becomes “10.11″.

That gives us:FloatFormula

It’s much more easy to convert from 8bit float then into. So for now this is the first part of the program. I think I’m going to add another class for the other conversion. I still feel that the code feels a bit messy, even if I have created a lot of smaller methods this time. It’s seem like I need to “design” a bit more.

This program does convert etc “01000010″ correctly into 0.125, but it’s not the standard following rules by float notation. The correct would be “00101000″ = 0.125 where we have a “1″ in the first part of the mantissa. (We are told we always have a 1 there, I’m not sure what decimal 0 do)

import java.text.DecimalFormat;

public class Bit8FloatToDec{

	// Generates all 256 possible bitsigns and test them against the method mantissa()
	public static void main( String [] args){

		DecimalFormat sevenDec = new DecimalFormat("0.0000000");
		String input = "";
		double output = 0;
		int counter = 0;

			for(int a = 0; a < 2; a++){
			for(int b = 0; b < 2; b++){
			for(int c = 0; c < 2; c++){
			for(int d = 0; d < 2; d++){
			for(int e = 0; e < 2; e++){
			for(int f = 0; f < 2; f++){
			for(int g = 0; g < 2; g++){
			for(int h = 0; h < 2; h++){
				input = a+""+b+""+c+""+d+""+e+""+f+""+g+""+h+"";
				output = mantissa(input);
				System.out.println("Number: " + counter + "\tBinary: " + input + "\tDecimal: " + sevenDec.format(output));
				counter++;
			}}}}}}}}//endfor

	}//endmain

	// Recives an "8bit" string and returns the decimal number
	// Etc "01011101" becomes "1.625"
	public static double mantissa(String input){

		double result = 0;
		int expo = expo(input);
		int i = 4, j = -1 + expo;

		// The charAt(i) increases, while the pow(2,j) decreases
		while(i != 8){
			if(input.charAt(i) == '1'){
				result += Math.pow(2,j);
			}//endif
			i++; j--;
		}//endwhile

		// If true, then negative number
		if(signbit(input)){
			result *= -1;
		}//endif

		return result;
	}//end mantissa()

	// Analyses the exponetinal and returns the starting value of it (-4 to +3)
	public static int expo(String input){

		int result = 0;
		int i = 3, j = 0;

		while(i != 0){
			if(input.charAt(i) == '1'){
				result += Math.pow(2,j);
			}
			j++;
			i--;
		}//endwhile

		return result -4;
	}//end expo()

	// Checks if it's an negative or positive number, return true if negative
	public static boolean signbit(String input){

		boolean result = false;

		if(input.charAt(0) == '1'){
			result = true;
		}//endif

		return result;
	}//end signbit
}//end class Bit8FloatToDec

As you can see from the output, we have a lot of repeating numbers like “00101000″ = “01000010″ = 0.125. The program is correct, even on numbers that is not following the standard. Most likely some are specified as special cases instead as “NaN” = “Not a Number” (That’s what you get when you are dividing something by 0). But I don’t know what they look like.

Number: 0       Binary: 00000000        Decimal: 0,0000000
Number: 1       Binary: 00000001        Decimal: 0,0039062
Number: 2       Binary: 00000010        Decimal: 0,0078125
Number: 3       Binary: 00000011        Decimal: 0,0117188
Number: 4       Binary: 00000100        Decimal: 0,0156250
Number: 5       Binary: 00000101        Decimal: 0,0195312
Number: 6       Binary: 00000110        Decimal: 0,0234375
Number: 7       Binary: 00000111        Decimal: 0,0273438
Number: 8       Binary: 00001000        Decimal: 0,0312500
Number: 9       Binary: 00001001        Decimal: 0,0351562
Number: 10      Binary: 00001010        Decimal: 0,0390625
Number: 11      Binary: 00001011        Decimal: 0,0429688
Number: 12      Binary: 00001100        Decimal: 0,0468750
Number: 13      Binary: 00001101        Decimal: 0,0507812
Number: 14      Binary: 00001110        Decimal: 0,0546875
Number: 15      Binary: 00001111        Decimal: 0,0585938
Number: 16      Binary: 00010000        Decimal: 0,0000000
Number: 17      Binary: 00010001        Decimal: 0,0078125
Number: 18      Binary: 00010010        Decimal: 0,0156250
Number: 19      Binary: 00010011        Decimal: 0,0234375
Number: 20      Binary: 00010100        Decimal: 0,0312500
Number: 21      Binary: 00010101        Decimal: 0,0390625
Number: 22      Binary: 00010110        Decimal: 0,0468750
Number: 23      Binary: 00010111        Decimal: 0,0546875
Number: 24      Binary: 00011000        Decimal: 0,0625000
Number: 25      Binary: 00011001        Decimal: 0,0703125
Number: 26      Binary: 00011010        Decimal: 0,0781250
Number: 27      Binary: 00011011        Decimal: 0,0859375
Number: 28      Binary: 00011100        Decimal: 0,0937500
Number: 29      Binary: 00011101        Decimal: 0,1015625
Number: 30      Binary: 00011110        Decimal: 0,1093750
Number: 31      Binary: 00011111        Decimal: 0,1171875
Number: 32      Binary: 00100000        Decimal: 0,0000000
Number: 33      Binary: 00100001        Decimal: 0,0156250
Number: 34      Binary: 00100010        Decimal: 0,0312500
Number: 35      Binary: 00100011        Decimal: 0,0468750
Number: 36      Binary: 00100100        Decimal: 0,0625000
Number: 37      Binary: 00100101        Decimal: 0,0781250
Number: 38      Binary: 00100110        Decimal: 0,0937500
Number: 39      Binary: 00100111        Decimal: 0,1093750
Number: 40      Binary: 00101000        Decimal: 0,1250000
Number: 41      Binary: 00101001        Decimal: 0,1406250
Number: 42      Binary: 00101010        Decimal: 0,1562500
Number: 43      Binary: 00101011        Decimal: 0,1718750
Number: 44      Binary: 00101100        Decimal: 0,1875000
Number: 45      Binary: 00101101        Decimal: 0,2031250
Number: 46      Binary: 00101110        Decimal: 0,2187500
Number: 47      Binary: 00101111        Decimal: 0,2343750
Number: 48      Binary: 00110000        Decimal: 0,0000000
Number: 49      Binary: 00110001        Decimal: 0,0312500
Number: 50      Binary: 00110010        Decimal: 0,0625000
Number: 51      Binary: 00110011        Decimal: 0,0937500
Number: 52      Binary: 00110100        Decimal: 0,1250000
Number: 53      Binary: 00110101        Decimal: 0,1562500
Number: 54      Binary: 00110110        Decimal: 0,1875000
Number: 55      Binary: 00110111        Decimal: 0,2187500
Number: 56      Binary: 00111000        Decimal: 0,2500000
Number: 57      Binary: 00111001        Decimal: 0,2812500
Number: 58      Binary: 00111010        Decimal: 0,3125000
Number: 59      Binary: 00111011        Decimal: 0,3437500
Number: 60      Binary: 00111100        Decimal: 0,3750000
Number: 61      Binary: 00111101        Decimal: 0,4062500
Number: 62      Binary: 00111110        Decimal: 0,4375000
Number: 63      Binary: 00111111        Decimal: 0,4687500
Number: 64      Binary: 01000000        Decimal: 0,0000000
Number: 65      Binary: 01000001        Decimal: 0,0625000
Number: 66      Binary: 01000010        Decimal: 0,1250000
Number: 67      Binary: 01000011        Decimal: 0,1875000
Number: 68      Binary: 01000100        Decimal: 0,2500000
Number: 69      Binary: 01000101        Decimal: 0,3125000
Number: 70      Binary: 01000110        Decimal: 0,3750000
Number: 71      Binary: 01000111        Decimal: 0,4375000
Number: 72      Binary: 01001000        Decimal: 0,5000000
Number: 73      Binary: 01001001        Decimal: 0,5625000
Number: 74      Binary: 01001010        Decimal: 0,6250000
Number: 75      Binary: 01001011        Decimal: 0,6875000
Number: 76      Binary: 01001100        Decimal: 0,7500000
Number: 77      Binary: 01001101        Decimal: 0,8125000
Number: 78      Binary: 01001110        Decimal: 0,8750000
Number: 79      Binary: 01001111        Decimal: 0,9375000
Number: 80      Binary: 01010000        Decimal: 0,0000000
Number: 81      Binary: 01010001        Decimal: 0,1250000
Number: 82      Binary: 01010010        Decimal: 0,2500000
Number: 83      Binary: 01010011        Decimal: 0,3750000
Number: 84      Binary: 01010100        Decimal: 0,5000000
Number: 85      Binary: 01010101        Decimal: 0,6250000
Number: 86      Binary: 01010110        Decimal: 0,7500000
Number: 87      Binary: 01010111        Decimal: 0,8750000
Number: 88      Binary: 01011000        Decimal: 1,0000000
Number: 89      Binary: 01011001        Decimal: 1,1250000
Number: 90      Binary: 01011010        Decimal: 1,2500000
Number: 91      Binary: 01011011        Decimal: 1,3750000
Number: 92      Binary: 01011100        Decimal: 1,5000000
Number: 93      Binary: 01011101        Decimal: 1,6250000
Number: 94      Binary: 01011110        Decimal: 1,7500000
Number: 95      Binary: 01011111        Decimal: 1,8750000
Number: 96      Binary: 01100000        Decimal: 0,0000000
Number: 97      Binary: 01100001        Decimal: 0,2500000
Number: 98      Binary: 01100010        Decimal: 0,5000000
Number: 99      Binary: 01100011        Decimal: 0,7500000
Number: 100     Binary: 01100100        Decimal: 1,0000000
Number: 101     Binary: 01100101        Decimal: 1,2500000
Number: 102     Binary: 01100110        Decimal: 1,5000000
Number: 103     Binary: 01100111        Decimal: 1,7500000
Number: 104     Binary: 01101000        Decimal: 2,0000000
Number: 105     Binary: 01101001        Decimal: 2,2500000
Number: 106     Binary: 01101010        Decimal: 2,5000000
Number: 107     Binary: 01101011        Decimal: 2,7500000
Number: 108     Binary: 01101100        Decimal: 3,0000000
Number: 109     Binary: 01101101        Decimal: 3,2500000
Number: 110     Binary: 01101110        Decimal: 3,5000000
Number: 111     Binary: 01101111        Decimal: 3,7500000
Number: 112     Binary: 01110000        Decimal: 0,0000000
Number: 113     Binary: 01110001        Decimal: 0,5000000
Number: 114     Binary: 01110010        Decimal: 1,0000000
Number: 115     Binary: 01110011        Decimal: 1,5000000
Number: 116     Binary: 01110100        Decimal: 2,0000000
Number: 117     Binary: 01110101        Decimal: 2,5000000
Number: 118     Binary: 01110110        Decimal: 3,0000000
Number: 119     Binary: 01110111        Decimal: 3,5000000
Number: 120     Binary: 01111000        Decimal: 4,0000000
Number: 121     Binary: 01111001        Decimal: 4,5000000
Number: 122     Binary: 01111010        Decimal: 5,0000000
Number: 123     Binary: 01111011        Decimal: 5,5000000
Number: 124     Binary: 01111100        Decimal: 6,0000000
Number: 125     Binary: 01111101        Decimal: 6,5000000
Number: 126     Binary: 01111110        Decimal: 7,0000000
Number: 127     Binary: 01111111        Decimal: 7,5000000
Number: 128     Binary: 10000000        Decimal: -0,0000000
Number: 129     Binary: 10000001        Decimal: -0,0039062
Number: 130     Binary: 10000010        Decimal: -0,0078125
Number: 131     Binary: 10000011        Decimal: -0,0117188
Number: 132     Binary: 10000100        Decimal: -0,0156250
Number: 133     Binary: 10000101        Decimal: -0,0195312
Number: 134     Binary: 10000110        Decimal: -0,0234375
Number: 135     Binary: 10000111        Decimal: -0,0273438
Number: 136     Binary: 10001000        Decimal: -0,0312500
Number: 137     Binary: 10001001        Decimal: -0,0351562
Number: 138     Binary: 10001010        Decimal: -0,0390625
Number: 139     Binary: 10001011        Decimal: -0,0429688
Number: 140     Binary: 10001100        Decimal: -0,0468750
Number: 141     Binary: 10001101        Decimal: -0,0507812
Number: 142     Binary: 10001110        Decimal: -0,0546875
Number: 143     Binary: 10001111        Decimal: -0,0585938
Number: 144     Binary: 10010000        Decimal: -0,0000000
Number: 145     Binary: 10010001        Decimal: -0,0078125
Number: 146     Binary: 10010010        Decimal: -0,0156250
Number: 147     Binary: 10010011        Decimal: -0,0234375
Number: 148     Binary: 10010100        Decimal: -0,0312500
Number: 149     Binary: 10010101        Decimal: -0,0390625
Number: 150     Binary: 10010110        Decimal: -0,0468750
Number: 151     Binary: 10010111        Decimal: -0,0546875
Number: 152     Binary: 10011000        Decimal: -0,0625000
Number: 153     Binary: 10011001        Decimal: -0,0703125
Number: 154     Binary: 10011010        Decimal: -0,0781250
Number: 155     Binary: 10011011        Decimal: -0,0859375
Number: 156     Binary: 10011100        Decimal: -0,0937500
Number: 157     Binary: 10011101        Decimal: -0,1015625
Number: 158     Binary: 10011110        Decimal: -0,1093750
Number: 159     Binary: 10011111        Decimal: -0,1171875
Number: 160     Binary: 10100000        Decimal: -0,0000000
Number: 161     Binary: 10100001        Decimal: -0,0156250
Number: 162     Binary: 10100010        Decimal: -0,0312500
Number: 163     Binary: 10100011        Decimal: -0,0468750
Number: 164     Binary: 10100100        Decimal: -0,0625000
Number: 165     Binary: 10100101        Decimal: -0,0781250
Number: 166     Binary: 10100110        Decimal: -0,0937500
Number: 167     Binary: 10100111        Decimal: -0,1093750
Number: 168     Binary: 10101000        Decimal: -0,1250000
Number: 169     Binary: 10101001        Decimal: -0,1406250
Number: 170     Binary: 10101010        Decimal: -0,1562500
Number: 171     Binary: 10101011        Decimal: -0,1718750
Number: 172     Binary: 10101100        Decimal: -0,1875000
Number: 173     Binary: 10101101        Decimal: -0,2031250
Number: 174     Binary: 10101110        Decimal: -0,2187500
Number: 175     Binary: 10101111        Decimal: -0,2343750
Number: 176     Binary: 10110000        Decimal: -0,0000000
Number: 177     Binary: 10110001        Decimal: -0,0312500
Number: 178     Binary: 10110010        Decimal: -0,0625000
Number: 179     Binary: 10110011        Decimal: -0,0937500
Number: 180     Binary: 10110100        Decimal: -0,1250000
Number: 181     Binary: 10110101        Decimal: -0,1562500
Number: 182     Binary: 10110110        Decimal: -0,1875000
Number: 183     Binary: 10110111        Decimal: -0,2187500
Number: 184     Binary: 10111000        Decimal: -0,2500000
Number: 185     Binary: 10111001        Decimal: -0,2812500
Number: 186     Binary: 10111010        Decimal: -0,3125000
Number: 187     Binary: 10111011        Decimal: -0,3437500
Number: 188     Binary: 10111100        Decimal: -0,3750000
Number: 189     Binary: 10111101        Decimal: -0,4062500
Number: 190     Binary: 10111110        Decimal: -0,4375000
Number: 191     Binary: 10111111        Decimal: -0,4687500
Number: 192     Binary: 11000000        Decimal: -0,0000000
Number: 193     Binary: 11000001        Decimal: -0,0625000
Number: 194     Binary: 11000010        Decimal: -0,1250000
Number: 195     Binary: 11000011        Decimal: -0,1875000
Number: 196     Binary: 11000100        Decimal: -0,2500000
Number: 197     Binary: 11000101        Decimal: -0,3125000
Number: 198     Binary: 11000110        Decimal: -0,3750000
Number: 199     Binary: 11000111        Decimal: -0,4375000
Number: 200     Binary: 11001000        Decimal: -0,5000000
Number: 201     Binary: 11001001        Decimal: -0,5625000
Number: 202     Binary: 11001010        Decimal: -0,6250000
Number: 203     Binary: 11001011        Decimal: -0,6875000
Number: 204     Binary: 11001100        Decimal: -0,7500000
Number: 205     Binary: 11001101        Decimal: -0,8125000
Number: 206     Binary: 11001110        Decimal: -0,8750000
Number: 207     Binary: 11001111        Decimal: -0,9375000
Number: 208     Binary: 11010000        Decimal: -0,0000000
Number: 209     Binary: 11010001        Decimal: -0,1250000
Number: 210     Binary: 11010010        Decimal: -0,2500000
Number: 211     Binary: 11010011        Decimal: -0,3750000
Number: 212     Binary: 11010100        Decimal: -0,5000000
Number: 213     Binary: 11010101        Decimal: -0,6250000
Number: 214     Binary: 11010110        Decimal: -0,7500000
Number: 215     Binary: 11010111        Decimal: -0,8750000
Number: 216     Binary: 11011000        Decimal: -1,0000000
Number: 217     Binary: 11011001        Decimal: -1,1250000
Number: 218     Binary: 11011010        Decimal: -1,2500000
Number: 219     Binary: 11011011        Decimal: -1,3750000
Number: 220     Binary: 11011100        Decimal: -1,5000000
Number: 221     Binary: 11011101        Decimal: -1,6250000
Number: 222     Binary: 11011110        Decimal: -1,7500000
Number: 223     Binary: 11011111        Decimal: -1,8750000
Number: 224     Binary: 11100000        Decimal: -0,0000000
Number: 225     Binary: 11100001        Decimal: -0,2500000
Number: 226     Binary: 11100010        Decimal: -0,5000000
Number: 227     Binary: 11100011        Decimal: -0,7500000
Number: 228     Binary: 11100100        Decimal: -1,0000000
Number: 229     Binary: 11100101        Decimal: -1,2500000
Number: 230     Binary: 11100110        Decimal: -1,5000000
Number: 231     Binary: 11100111        Decimal: -1,7500000
Number: 232     Binary: 11101000        Decimal: -2,0000000
Number: 233     Binary: 11101001        Decimal: -2,2500000
Number: 234     Binary: 11101010        Decimal: -2,5000000
Number: 235     Binary: 11101011        Decimal: -2,7500000
Number: 236     Binary: 11101100        Decimal: -3,0000000
Number: 237     Binary: 11101101        Decimal: -3,2500000
Number: 238     Binary: 11101110        Decimal: -3,5000000
Number: 239     Binary: 11101111        Decimal: -3,7500000
Number: 240     Binary: 11110000        Decimal: -0,0000000
Number: 241     Binary: 11110001        Decimal: -0,5000000
Number: 242     Binary: 11110010        Decimal: -1,0000000
Number: 243     Binary: 11110011        Decimal: -1,5000000
Number: 244     Binary: 11110100        Decimal: -2,0000000
Number: 245     Binary: 11110101        Decimal: -2,5000000
Number: 246     Binary: 11110110        Decimal: -3,0000000
Number: 247     Binary: 11110111        Decimal: -3,5000000
Number: 248     Binary: 11111000        Decimal: -4,0000000
Number: 249     Binary: 11111001        Decimal: -4,5000000
Number: 250     Binary: 11111010        Decimal: -5,0000000
Number: 251     Binary: 11111011        Decimal: -5,5000000
Number: 252     Binary: 11111100        Decimal: -6,0000000
Number: 253     Binary: 11111101        Decimal: -6,5000000
Number: 254     Binary: 11111110        Decimal: -7,0000000
Number: 255     Binary: 11111111        Decimal: -7,5000000

// I admit it was fun to use code for the first time to generate code. It would be boring if I had to edit all those for-loops by hand. I love the }}}}}}}}//endfor ending. ^^
// I use to much “to” in this post. ಠ_ಠ

for(int i = 0; i < 8; i++){
	System.out.println("for(int "+ (char)(i+97) + " = 0; " + (char)(i+97) + " < 2; " + (char)(i+97) + "++){");
}

0 comments ↓

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment