Yesterday I created trying to understand why I was getting a different value
casting an address and just casting a float ..
anyway the reason for this is this code here
Code:
#include<iostream>
using namespace std;
void binaryPrint(unsigned char * buff, int size)
{
for (int i = (size -1) ; i >= 0 ; i --)
{
unsigned char mask = 128;
for (int j = 0 ; j < 8 ; j ++)
{
cout << ((buff[i] & mask) ? "1" : "0");
mask = mask >> 1;
}
}
cout << endl;
}
void main()
{
float i;
i = -100.75f;
binaryPrint( (unsigned char * ) & i, sizeof(float));
}
My teacher wrote this in class because wanted us to understand how floating point numbers are stored.
But I find it really hard to understand his code.
firstly how is it possible to store an integer into a char type?
is it because all numbers in computer base form is in binary? and because 128 is not greater than 255 the highest number an 8bit can take, I was able to store in a char?
second:
when i declare a type and a location is made for it in memory is it contiguous, so it like an array ?
so for an int, because memory is usually stored in bytes
it will divide and int of size 4bytes into each byte
so it is a 4 by 8 array?
1111 1111
1111 1111
1111 1111
1111 1111
so if i just declared a simple char , because a char size is already a byte it doesn't have divide into up into separate byte..so it is basically
1111 1111
if that is the case
this piece of code:
Code:
void binaryPrint(unsigned char * buff, int size)
{
for (int i = (size -1) ; i >= 0 ; i --)
{
unsigned char mask = 128;
for (int j = 0 ; j < 8 ; j ++)
{
cout << ((buff[i] & mask) ? "1" : "0");
mask = mask >> 1;
}
}
cout << endl;
}
If bytes are located as arrays, 1d array, 2d array..
were 2 loops used because char *buff is string pointer to an address that holds one byte of data
I don't really understand why i will be initialised to the size of a type..why not start from 0?
when he did buff[i]
so if i have 32 bits in memory:
1000 1111
1100 0001
1001 1011
0011 0101
if i did buff[i]
since i is 4-1 and represents row 4
i am assuming
it just takes
0011 0101 & 1000 0000
0011 0101 & 0100 0000
0011 0101 & 0010 0000
and just ands them together to see which bit is set.after the inner loop is done
it goes to row 3
and does the process again. So it does this 32 times.
so let's say instead of converting a float or int - since they are 32bits
i wanted to convert a char
now a char is 8 bits long
1111 0000
if i had a string pointer to an address in memory that holds the binary digits
so buff points to 1111 0000
1111 0000 is essentially a 1d array ..
so why is that when I use just one loop, to check which bits are set it doesn't work ?
Code:
#include <iostream>
using namespace std ;
void main()
{
char let = 9 ;
unsigned char* buff = (unsigned char*)&let;
unsigned char mask = 128 ;
int i= 0;
for(; i<8; i++)
{
if(buff[i] & mask)
{
cout << "1" ;
}
else
{
cout << "0" ;
}
mask >>=1 ;
}
cout << endl ;
system("pause") ;
}
... Unless
because I am incrementing i..
i am moving to the next row of byte in memory...
so that wouldn't work..
i would have to do buff[0] or *buff for it to work...
please help explain..