Binary Converter - Bit level

This is a discussion on Binary Converter - Bit level within the C++ Programming forums, part of the General Programming Boards category; Yesterday I created trying to understand why I was getting a different value casting an address and just casting a ...

  1. #1
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629

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

  2. #2
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    can someone please at least some of the questions for me? Thanks

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,807
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    [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?

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    3,300
    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

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,807
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by jimblumberg View Post
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary to 32 bit floating point
    By TenTierHook in forum C++ Programming
    Replies: 6
    Last Post: 10-14-2010, 06:23 AM
  2. c# OpenSubKey problem on 64 bit windows vs 32 bit.
    By Striph in forum C# Programming
    Replies: 3
    Last Post: 03-22-2010, 06:07 AM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  4. how to pack 8 x 4 bit binary numbers into a long int?
    By n00bcodezor in forum C Programming
    Replies: 11
    Last Post: 11-19-2001, 04:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21