I am trying to read temperature data from a temperature conditioning chip and it says that the data is formatted as the first 14-bits of a 32 bit number. I understand the sign and the main part and the decimal part. But I cannot understand bit number 20. I thought it was a decimal place holder, but its value changes when they gave examples for the format. You can see that for +1600.00 --> 0110 0100 0000 00 and for +25.0 --> 0000 0001 1001 00. What does the 3rd bit from the left represent??
D20 is the 1's position (2^0).
D31 = Sign bit
D30 = 2^10 = 1024
D29 = 2^9 = 512
D28 = 2^8 = 256
D27 = 2^7 = 128
D26 = 2^6 = 64
D25 = 2^5 = 32
D24 = 2^4 = 16
D23 = 2^3 = 8
D22 = 2^2 = 4
D21 = 2^1 = 2
D20 = 2^0 = 1
D19 = 2^-1 = 0.5
D18 = 2^-2 = 0.25
Related
So the exercise says: "Consider binary encoding of real numbers on 16 bits. Fill the empty points of the binary encoding of the number -0.625 knowing that "1110" stands for the exposant and is minus one "-1"
_ 1110_ _ _ _ _ _ _ _ _ _ _ "
I can't find the answer and I know this is not a hard exercise (at least it doesn't look like a hard one).
Let's ignore the sign for now, and decompose the value 0.625 into (negative) powers of 2:
0.625(dec) = 5 * 0.125 = 5 * 1/8 = 0.101(bin) * 2^0
This should be normalized (value shifted left until there is a one before the decimal point, and exponent adjusted accordingly), so it becomes
0.625(dec) = 1.01(bin) * 2^-1 (or 1.25 * 0.5)
With hidden bit
Assuming you have a hidden bit scenario (meaning that, for normalized values, the top bit is always 1, so it is not stored), this becomes .01 filled up on the right with zero bits, so you get
sign = 1 -- 1 bit
exponent = 1110 -- 4 bits
significand = 0100 0000 000 -- 11 bits
So the bits are:
1 1110 01000000000
Grouped differently:
1111 0010 0000 0000(bin) or F200(hex)
Without hidden bit (i.e. top bit stored)
If there is no hidden bit scenario, it becomes
1 1110 10100000000
or
1111 0101 0000 0000(bin) = F500(hex)
First of all you need to understand that each number "z" can be represented by
z = m * b^e
m = Mantissa, b = bias, e = exponent
So -0.625 could be represented as:
-0.625 * 10^ 0
-6,25 * 10^-1
-62,5 * 10^-2
-0,0625 * 10^ 1
With the IEEE conversion we aim for the normalized floating point number which means there is only one preceding number before the comma (-6,25 * 10^-1)
In binary the single number before the comma will always be a 1, so this number will not be stored.
You're converting into a 16 bit float so you have:
1 Bit sign 5 Bits Exponent 10 Bits mantissa == 16Bits
Since the exponent can be negative and positive (as you've seen above this depends only on the comma shifting) they came up with the so called bias. For 5 bits the bias value is 01 111 == 15(dez) with 14 beeing ^-1 and 16 beeing ^1 ...
Ok enough small talk lets convert your number as an example to show the process of conversion:
Convert the pre-decimal position to binary as always
Multiply the decimal place by 2 if the result is greater 1, subtract 1 and notate 1 if it's smaller 0 notate 0.
Proceed this step until the result is == 0 or you've notated as many numbers as your mantissa has
shift the comma to only one pre-decimal and count the shiftings. if you shifted to the left add the count to the bias if you have to shift to the right subtract the count from the bias. This is your exponent
Dertmine your sign and add all parts together
-0.625
1. 0 to binary == 0
2. 0.625 * 2 = 1.25 ==> -1
0.25 * 2 = 0.5 ==> 0
0.5 * 2 = 1 ==> -1
Abort
3. The intermediary result therefore is -0.101
shift the comma 1 times to the right for a normalized floating point number:
-1.01
exponent = bias + (-1) == 15 - 1 == 14(dez) == 01110(bin)
4. put the parts together, sign = 1(negative), (and remember we do not store the leading 1 of number)
1 01110 01
since we aborted during our mantissa calculation fill the rest of the bits with 0:
1 01110 01 000 000 00
The IEEE 754 standard specifies a binary16 as having the following format:
Sign bit: 1 bit
Exponent width: 5 bits
Significand precision: 11 bits (10 explicitly stored)
Equation = exp(-1, signbit) x exp(2, exponent-15) x (1.significantbits)
Solution is as follows,
-0.625 = -1 x 0.5 x 1.25
significant bits = 25 = 11001
exponent = 14 = 01110
signbit = 1
ans = (1)(01110)(0000011001)
I am converting the hexadecimal number 0XA85D10 to binary. However, I am told you can convert this number without using paper or a calculator.
Is this possible?
Thanks
The easiest way to convert from hex to binary is to split each digit in the hex into an 4 digit binary number. (i.e. D matches to 1110)
Using your example
A 8 5 D 1 0
1010 1000 0101 1110 0001 0000
Yes. It is possible. One hexadecimal digit is exactly four binary digits.
A = 1010
8 = 1000
...and so on.
If the digit is greater or equal to 8, then subtract 8 from the digit the first binary digit is 1, otherwise it is zero.
If the digit is now greater or equal to 4, then subtract 4 and the next digit out is 1, otherwise the next digit out is 0.
If the digit is now greater or equal to 2, then subtract 2 and the next digit out is 1, otherwise the next digit out is 0.
Whatever is left, 0 or 1, is the remaining digit.
Easily. Each digit in a hex number translates to 4 digits in a binary number, so you only need to know the binary numbers from 0 to f, or 0000 to 1111.
As an example:
0xc3e2
c = 12 decimal = 1100
3 = 0011
e = 14 decimal = 1110
2 = 0010
Then just string them together.
0xc3e2 = 1100001111100010 binary
You can convert Hexadecimal to Binary using the following (Hex -> Binary):
0 = 0000
1 = 0001
2 = 0010
3 = 0011
4 = 0100
5 = 0101
6 = 0110
7 = 0111
8 = 1000
9 = 1001
A = 1010
B = 1011
C = 1100
D = 1101
E = 1110
F = 1111
Hope this helps!
public with sharing class HexToBin {
private String code;
public String result;
Map<String, String> hexToBinMap = new Map<String, String>{'0'=>'0000','1'=>'0001','2'=>'0010','3'=>'0011','4'=>'0100','5'=>'0101','6'=>'0110','7'=>'0111','8'=>'1000','9'=>'1001','A'=>'1010','B'=>'1011','C'=>'1100','D'=>'1101','E'=>'1110','F'=>'1111'};
public HexToBin(String code) {
this.code = code;
result = getResult(code);
}
private String hexToBin(String hex){
if(hex.length()==1){
return hexToBinMap.get(hex);
}
return '';
}
private String getResult (String code) {
String res ='';
for(Integer i=0; i<code.length(); i++){
res = res + hexToBin(code.substring(i,i+1));
}
return res;
}
}
I'm a CS freshman and I find the division way of finding a binary number to be a pain. Is it possible to use log to quickly find 24, for instance, in binary?
If you want to use logarithms, you can.
Define log2(b) as log(b) / log(2) or ln(b) / ln(2) (they are the same).
Repeat the following:
Define n as the integer part of log2(b). There is a 1 in the nth position in the binary representation of b.
Set b = b - 2n
Repeat first step until b = 0.
Worked example: Converting 2835 to binary
log2(2835) = 11.47.. => n = 11
The binary representation has a 1 in the 211 position.
2835 - (211 = 2048) = 787
log2(787) = 9.62... => n = 9
The binary representation has a 1 in the 29 position.
787 - (29 = 512) = 275
log2(275) = 8.10... => n = 8
The binary representation has a 1 in the 28 position.
275 - (28 = 256) = 19
log2(19) = 4.25... => n = 4
The binary representation has a 1 in the 24 position.
19 - (24 = 16) = 3
log2(3) = 1.58.. => n = 1
The binary representation has a 1 in the 21 position.
3 - (21 = 2) = 1
log2(1) = 0 => n = 0
The binary representation has a 1 in the 20 position.
We know the binary representation has 1s in the 211, 29, 28, 24, 21, and 20 positions:
2^ 11 10 9 8 7 6 5 4 3 2 1 0
binary 1 0 1 1 0 0 0 1 0 0 1 1
so the binary representation of 2835 is 101100010011.
From a CS perspective, binary is quite easy because you usually only need to go up to 255. Or 15 if using HEX notation. The more you use it, the easier it gets.
How I do it on the fly, is by remembering all the 2 powers up to 128 and including 1. (The presence of the 1 instead of 1.4xxx possibly means that you can't use logs).
128,64,32,16,8,4,2,1
Then I use the rule that if the number is bigger than each power in descending order, that is a '1' and subtract it, else it's a '0'.
So 163
163 >= 128 = '1' R 35
35 !>= 64 = '0'
35 >= 32 = '1' R 3
3 !>= 16 = '0'
3 !>= 8 = '0'
3 !>= 4 = '0'
3 >= 2 = '1' R 1
1 >= 1 = '1' R 0
163 = 10100011.
It may not be the most elegant method, but when you just need to convert something ad-hoc thinking of it as comparison and subtraction may be easier than division.
Yes, you have to loop through 0 -> power which is bigger than you need and then take the remainder and do the same, which is a pain too.
I would suggest you trying recursion approach of division called 'Divide and Conquer'.
http://web.stanford.edu/class/archive/cs/cs161/cs161.1138/lectures/05/Small05.pdf
But again, since you need a binary representation, I guess unless you use ready utils, division approach is the simplest one IMHO.
I have to represent this equation 2049*(M+N*100)+(M*N)*2800 using John Napier's Location Arithmetic and then calculate it using the notation's characteristic.
So for example I know that 29*11 would be (2*1)(2*1)(9*1)(9*1) = 3 3 10 10 = 4 11 = 24+ 211 = 16 + 2048 = 2064.
With that knowledge I can change most part of equation, but how can I calculate it with a big number like 2800?
Let's say M = 7 and N = 10.
I am currently reading a book about "bit fiddling" and the following formula appears:
x-y = x+¬y+1
But this doesn't seem to work. Example:
x = 0100
y = 0010
x-y = 0010
¬y = 1101
¬y+1 = 1110
x+1110 = 10010
But 10010 != 0010...
Where did I make a mistake (if any)?
(The book is "Hacker's Delight" by Henry S. Warren.)
You only have a four bit system! That extra 1 on the left of your final result can't exist. It should be:
x = 0100
y = 0010
~y = 1101
~y + 1 = 1110
x + 1110 = 0010
The other bit overflows, and isn't part of your result. You may want to read up on two's complement arithmetic.
You are carrying the extra bit. In real computers if you overflow the word, the bit disappears. (actually it gets saved in a carry flag.) .
Assuming the numbers are constrained to 4 bits, then the fifth 1 would be truncated, leaving you with 0010.
It's all about overflow. You only have four bits, so it's not 10010, but 0010.
Just to add to the answers, in a 2's complement system:
~x + 1 = -x
Say x = 2. In 4 bits, that's 0010.
~x = 1101
~x + 1 = 1110
And 1110 is -2