Dear all,
I have recently come upon an interesting problem: to write a program to access the computer's memory to display the stored bit patterns for arbitrary float variables. I know that here it is not accepted to post problems for others to solve. In fact I would just like to ask if anyone could give me a hint or two to help me start on the right tracks.
I know that using a pointer you get the value of the address to another variable:
1 2 3 4
int i;
int *p;
p = &i; //assign the address value to pointer p
Once you have the address location of a variable, say a float, how can you then display the individual bit patterns? A float is divided in [sign - 1 bit][exponent - 8 bits] and [mantissa - 23 bits]. How can you display the individual bits?
You can use bitwise operations to get the individual values of each bit. For example:
1 2 3 4
float a = 32.3;
std::cout<< a&1 <<std::endl; // gets the least significant bit
std::cout<< a&2 <<std::endl; // next most significant
std::cout<< a&4 <<std::endl; // and so on
Thank you for your reply. I have tried compiling the above code using Geany on Ubuntu, but i am receiving the following compiler error: "error: invalid operands of types ‘int’ and ‘<unresolved overloaded function type>’ to binary ‘operator<<’". Am I omitting some header inclusions? There seems to be an incorrect conversion/operation happening here.