Thread: Converting Integers to Binary

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    6

    Cool Converting Integers to Binary

    Hi Im a newbee at this and Im in a bit of trouble converting Integers to Binary numbers I know I have to use a function but I have no idea how to set up this function so that it could do this thanx in adavance

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    44
    Use a cast.Im quite rusty and a newbie to begin with but I believe you want this(this is an example):


    int i=1;
    bool b;
    b=<bool>i;

    I believe that should work,and all types have casts:

    char g="T"
    int ASCII;
    ASCII=<int>g;

    int f=104;
    float t;
    t=<float>f;

    if it doesnt work,ask around about casts
    ego sum via et veritas et vitae -Jesus Christ

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The following is one way to do it.
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    #define WYSIWYG(x)  #x, (x)
    
    char *bits_uint(char *dest, unsigned int value)
    {
       char *start = dest;
       if(dest)
       {
          unsigned int mask;
          for(mask = (-1U >> 1) + 1; mask; mask >>= 1)
          {
             *dest++ = value & mask ? '1' : '0';
          }
          *dest = 0;
       }
       return(start);
    }
    
    int main(void)
    {
       unsigned int value = 125;
       char binary[sizeof(value) * CHAR_BIT + 1];
       bits_uint(binary, value);
       printf("%s = %u = %X\n", WYSIWYG(value), value);
       printf("%s = \"%s\"\n", WYSIWYG(binary));
       return(0);
    }
    
    /* my output
    value = 125 = 7D
    binary = "00000000000000000000000001111101"
    */
    There is a bit going on there; I hope you ask questions on anything that does not make sense. I guess I'm trying to say I hope this isn't just a homework question that you'll take the code and run with.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    6

    Cool Thanx

    thanx for the help will be usefull thanx gone from completely lost to having an idea were to start

  5. #5
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Your integer is already stored in binary format. If you simply want to print it out and not store it in a string, you can likewise do this:

    Code:
    #include <stdio.h>
    
    int main()
    {
       int i;
       int value = 125;
    
       for(i = 0; i < 32; i++)
          printf("%d", (value >> (31 - i)) & 1);
       printf("\n");
       return 0;
    }
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  6. #6
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    He Cshot,

    You should use the sizeof operator to make your code more portable.

    Cheers,
    Monster

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    44

    Post

    O,I thought you meant you wanted integer to bool,sory.
    ego sum via et veritas et vitae -Jesus Christ

  8. #8
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    >> You should use the sizeof operator to make your code more portable.
    Yea I should've. I was just demonstrating the concept and didn't want to confuse him.

    Here's with sizeof:
    Code:
    #include <stdio.h>
    
    int main()
    {
       int i;
       int value = 125;
    
       for(i = 0; i < sizeof(int)*8; i++)
          printf("%d", (value >> (sizeof(int)*8 - i)) & 1);
       printf("\n");
       return 0;
    }
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Cshot's code does not appear to be correct.
    Code:
    #include <stdio.h>
    
    int main()
    {
       int i;
       int value = 125;
    
       for(i = 0; i < sizeof(int)*8; i++)
          printf("%d", (value >> (sizeof(int)*8 /* - 1 */ - i)) & 1);
       printf("\n");
       return 0;
    }
    
    /* my output
    10000000000000000000000000111110
    */
    I would like to point out that a char does not necessarily contain 8 bits, so the 8 is not very portable.

    Also, an integer /may/ include padding bits, so using sizeof(int) * CHAR_BIT is not even fully portable.

    Using unsigned types would be preferable when using bit shifts.

    And as an individual preference, I like to do sizeof(value) instead of sizeof(int) -- I try to avoid the middleman of assuming the underlying type.
    Last edited by Dave_Sinkula; 10-03-2002 at 12:32 PM.

  10. #10
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Missed the extra -1 in this line:
    printf("%d", (value >> (sizeof(int)*8 - i)) & 1);
    No sleep = lots of mistakes

    >> Also, an integer /may/ include padding bits, so using sizeof(int) * CHAR_BIT is not even fully portable.
    There may be padding?
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    > Also, an integer /may/ include padding bits
    Huh?
    An int has to be an integral multiple of the number of bits in a char, since sizeof(char) is by definition 1, and all sizeof values are ints
    Got any references?
    While an int must be an integral multiple of the number of bits in a char, not all bits are required to represent a value. (I think having an additional parity bit is an example I have heard of.) The exception is an unsigned char. Sorry, I don't have a reference right now.

    [EDIT]
    Reference: ISO/IEC 9899-1999 (C99)
    6.2.6.2 Integer types
    1 For unsigned integer types other than unsigned char, the bits of the object representation shall be divided into two groups: value bits and padding bits (there need not be any of the latter). If there are N value bits, each bit shall represent a different power of 2 between 1 and 2N - 1, so that objects of that type shall be capable of representing values from 0 to 2N - 1 using a pure binary representation; this shall be known as the value representation. The values of any padding bits are unspecified.44)

    44) Some combinations of padding bits might generate trap representations, for example, if one padding bit is a parity bit. Regardless, no arithmetic operation on valid values can generate a trap representation other than as part of an exceptional condition such as an overflow, and this cannot occur with unsigned types. All other combinations of padding bits are alternative object representations of the value specified by the value bits.
    [/EDIT]
    Last edited by Dave_Sinkula; 10-03-2002 at 06:43 PM.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I actually like CShot's orginal version. If the integer is shorter than 32, it will just add some extra zeros on the front.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I would like to point out that a char does not necessarily contain 8 bits, so the 8 is not very portable.
    sizeof() returns the size in bytes. And if you multiply by 8 you get the number of bits.

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I actually like CShot's orginal version. If the integer is shorter than 32, it will just add some extra zeros on the front.
    And if it's bigger than 32, it'll be wrong.
    sizeof() returns the size in bytes. And if you multiply by 8 you get the number of bits.
    No. In C, a byte does not necessarily contain 8 bits; a byte contains CHAR_BIT bits, and CHAR_BIT might not be 8.

  15. #15
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    >> No. In C, a byte does not necessarily contain 8 bits; a byte contains CHAR_BIT bits, and CHAR_BIT might not be 8.
    I was just wondering if you've seen examples where a CHAR_BIT wasn't 8. However, I think I've seen a word contain 14 bits before.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting 32 bit binary IP to decimal IP (vice-versa)
    By Mankthetank19 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 07:17 PM
  2. Converting From Binary Tree to Threaded Binary Trees
    By elton_fan in forum C Programming
    Replies: 15
    Last Post: 11-08-2007, 11:41 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Converting Sign Magnitude Integer Binary Files
    By Arthur Dent in forum C Programming
    Replies: 7
    Last Post: 09-13-2004, 10:07 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM