# Binary Converter - Bit level

• 10-17-2010
Eman
Binary Converter - Bit level
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 --)
{
for (int j = 0 ; j < 8 ; j ++)
{
cout << ((buff[i] & mask) ? "1" : "0");
}
}
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 --)
{
for (int j = 0 ; j < 8 ; j ++)
{
cout << ((buff[i] & mask) ? "1" : "0");
}
}
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++)
{

{
cout << "1" ;
}
else
{
cout << "0" ;
}

}

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...

• 10-17-2010
Eman
can someone please at least some of the questions for me? Thanks
• 10-17-2010
laserlight
Quote:

Originally Posted by Eman
firstly how is it possible to store an integer into a char type?

Where do you see this happening? Note that chars are integers, though somewhat special case integers. Note also that a char is not a char pointer, so you must be accurate about what you are talking about.

Quote:

Originally Posted by Eman
when i declare a type and a location is made for it in memory is it contiguous, so it like an array ?

Yes.

Quote:

Originally Posted by Eman
I don't really understand why i will be initialised to the size of a type..why not start from 0?

You can, but there is the matter of big-endian versus little-endian byte ordering. Why not change the code to start from 0, and then observe the result? Try this with an int instead of a float so that it will be easier to understand.
• 10-17-2010
Eman
[QUOTE=laserlight;976651]Where do you see this happening? Note that chars are integers, though somewhat special case integers. Note also that a char is not a char pointer, so you must be accurate about what you are talking about.

um, I didn't say char is a pointer..
i said

char *buff is a pointer to an address that holds a string of one byte..
anyways i'm not too big on terminology.

By special case integers. Do you mean Ascii and all?

So the difference between a char and a int really is just the amount bytes?
• 10-17-2010
jimblumberg
Quote:

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?
You are not storing an integer/floating point type in a character type. You are passing into your binaryPrint() a char pointer that points to the location holding your floating point number.

Jim
• 10-17-2010
laserlight
Quote:

Originally Posted by Eman
um, I didn't say char is a pointer..

I didn't say that you said that char is a pointer ;)

Quote:

Originally Posted by Eman
By special case integers. Do you mean Ascii and all?

In a way; I am thinking of what happens when you print a char as opposed to printing an int, using standard library functions, as well as the fact that sizeof(char) == 1 is always true.

Quote:

Originally Posted by Eman
So the difference between a char and a int really is just the amount bytes?

No. The difference is the type. An int is permitted to have the same number of bytes as a char, i.e., exactly one byte.
• 10-17-2010
Eman
Quote:

Originally Posted by jimblumberg
You are not storing an integer/floating point type in a character type. You are passing into your binaryPrint() a char pointer that points to the location holding your floating point number.

Jim

haha lovely explanation :O,
yeah I get that, thanks.

But interesting, how you point that out but not explain the rest :)