Extracting Sign, Exponent, and Mantissa from 64-bit Machine Number

In summary: For example, 'bin2dec('10001010101')' would give you the decimal value of 1109.5. Returning a value from a function: To return a value from a function in MATLAB, you need to use the 'return' keyword followed by the value you want to return. So, in your case, the function would look like this:function [dec] = Convert2to10(Ibase2)dec = 0; % initialize dec to 0for i = 1 : length(Ibase2) dec = dec + str2num(Ibase2(i)) * 2^(length(Ibase2) - i);endendIn summary, the
  • #1
Hiche
84
0

Homework Statement



The problem asks us to write a MATLAB function that takes as input a vector 'v' representing a 64-bit machine number in double precision, and extract the sign (t), the exponent (c), and the mantissa (f).

Homework Equations


The Attempt at a Solution



I'm extremely novice at MATLAB and the instructor assigned us a HW and a tutorial without bothering to pass on lectures about the language so bear with me, please.

Code:
function [t c f] = GetVector(v)
t = v(1);
c = v(2:12);
f = v(13:64);
end

Now, t will take the values 0 or 1 (the first element of the 64-bit representation), c will take the next 11 bits (the biased exponent), and f takes the remaining 52 bits. Now, my question is, do we have to subtract 1023 from the biased exponent to get e (the exponent) since they asked for the exponent? How exactly do we do that?

I think we have to create another function that converts the binary representation of c into decimal form then subtract 1023 from the 10-based number. If so, will this function do? And how exactly do functions return a value in MATLAB?

Code:
function [dec] = Convert2to10(Ibase2)
for i = 1 : length(Ibase2)
    dec = dec + str2num(Ibase2(i)) * 2^(length(Ibase2) - i);
end
end

Thanks in advance.
 
Physics news on Phys.org
  • #2


Hello there,

I can help you with your questions and provide some guidance on how to approach this problem. First, let's break down the problem into smaller steps:

1. Understanding the input: The input 'v' is a vector representing a 64-bit machine number in double precision. This means that 'v' contains 64 elements, each representing a single bit in the binary representation of the number. So, for example, if 'v' is [1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0], this represents the binary number 1010101010101010101010101010101010101010101010101010101010101010.

2. Extracting the sign, exponent, and mantissa: The sign bit, represented by 't', is simply the first element of the vector 'v'. The exponent, represented by 'c', is the next 11 bits after the sign bit (bits 2 to 12). The mantissa, represented by 'f', is the remaining 52 bits (bits 13 to 64). So, you are correct in your attempt at the solution.

3. Converting the exponent to decimal form: The exponent in the 64-bit representation is biased, which means that it is not the actual exponent of the number. To get the actual exponent, we need to subtract the bias from the exponent. In this case, the bias is 1023. So, if the exponent is 10001010101 (in binary), the actual exponent would be 10001010101 - 1023 = 1.

4. Converting binary to decimal: Your function to convert binary to decimal looks correct, but it would be better to use MATLAB's built-in
 

1. How do you extract the sign from a 64-bit machine number?

The sign of a 64-bit machine number can be extracted by looking at the most significant bit (MSB) of the number. If the MSB is 0, then the number is positive, and if the MSB is 1, then the number is negative.

2. What is the exponent of a 64-bit machine number?

The exponent of a 64-bit machine number is a binary number that represents the power of 2 by which the mantissa (fractional part) needs to be multiplied to get the actual value of the number. It is usually located between the sign bit and the mantissa.

3. How do you extract the exponent from a 64-bit machine number?

The exponent of a 64-bit machine number can be extracted by removing the sign bit and the mantissa from the number and converting the remaining bits into a decimal number. This decimal number is the exponent in binary form, which can then be converted into its decimal equivalent.

4. What is the mantissa of a 64-bit machine number?

The mantissa of a 64-bit machine number is the fractional part of the number. It represents the decimal digits after the decimal point in a decimal number. In a 64-bit machine number, the mantissa is usually located after the exponent and before the implied leading 1 bit.

5. How do you extract the mantissa from a 64-bit machine number?

The mantissa of a 64-bit machine number can be extracted by removing the sign bit and the exponent from the number and keeping the remaining bits as they are. These bits represent the mantissa in binary form, which can then be converted into its decimal equivalent.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
3K
  • Computing and Technology
Replies
4
Views
743
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
876
  • Set Theory, Logic, Probability, Statistics
Replies
5
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
5K
  • Programming and Computer Science
Replies
30
Views
4K
  • Introductory Physics Homework Help
Replies
6
Views
727
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top