Thread: Binary Representation Code

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    5

    Binary Representation Code

    Hi Guys,

    Does any one see any vulnerability with this code. Pls guide.

    Thanks.


    Code:
    #include <string.h>
    #include <stdio.h>
    
    void mydec2bin(int integer);
    
    int main(void)
    {
        int integer = 123;
    
        mydec2bin(integer);
    
    
        printf("\n\n\n");
        return 0;
    }
    
    void mydec2bin(int integer)
    {
        unsigned int bit_pos, mask, word, nibble_space = 0;
        printf("The binary representation of %d is as follows:\n\n",integer);
    
        word = 8 * sizeof(int);
        mask = 1;
        mask = mask << (word - 1);
    
    
    
        for(bit_pos= 0; bit_pos<=word-1; bit_pos++)//for each bit position
        {
    
            if(nibble_space == 4)
            {
                printf("  ");
                nibble_space = 0;
            }
    
            if(integer & mask)
                printf("1");
            else
                printf("0");
    
            mask = mask >> 1;
    
            nibble_space++;
        }
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Only that you should
    #include <limits.h>

    and do this
    word = CHAR_BIT * sizeof(int);

    Other than that, seems good.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by passionate_guy
    Does any one see any vulnerability with this code. Pls guide.
    Code:
        word = 8 * sizeof(int);
    Each byte contains CHAR_BIT bits, not necessarily 8; an int may contain padding bits.
    Last edited by Dave_Sinkula; 01-22-2006 at 10:36 AM. Reason: D'oh! Pokey again. :(
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    >an int may contain padding bits.
    Sorry Mr. Sinkula, but I'm not going to let you off the hook that easily. I believe that you're talking about trap representations and how the number of representation bits in anything but an unsigned char could be different than the total bit size of the type, which makes CHAR_BIT * sizeof ( T ) where T is anything but unsigned char a dangerous proposition. That's cool. I understand the problem and it's hard to explain to someone who doesn't also understand it, but you didn't give a viable alternative that works.

    I'm a firm believer of only addressing a problem if you have a solution that's as good or better than the existing solution.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    >http://www.daniweb.com/code/snippet148.html
    Heheh, cop out.

    That's about what I was thinking, though it doesn't accurately give you the value bits for a signed type. If you use one of the MAX macros from limits.h, you can get a loop that's useful for signed as well as unsigned types:
    Code:
    int n = 0;
    
    for ( bit = INT_MAX; bit > 0; bit &= bit - 1 )
      ++n;
    But even though that's a readable and correct way to get the number of value bits for a type, it's not really better than CHAR_BIT * sizeof(T) because it can't be optimized away into a compile-time constant. One of the cooler macros I've seen gives you the value bits of any 2^N-1 value. That includes the MAX macros in limits.h, so you can easily find the value bits of any integral data type.
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    /* Brain hurting macro that I wish I had thought of */
    #define value_bits(x) ((x) / ((x) % 0x3fffffffL + 1) /0x3fffffffL % 0x3fffffffL \
      * 30 + (x) % 0x3fffffffL / ((x) % 31 + 1) / 31 % 31 * 5 + 4 - 12 / ((x) % 31 + 3))
    
    #define MAX_SIZE INT_MAX
    
    int main ( void )
    {
      unsigned int i, n = 0;
    
      puts ( "value_bits says:" );
      printf ( "%d representation bits\n", value_bits ( MAX_SIZE ) );
    
      for ( i = MAX_SIZE; i > 0; i &= i - 1 )
        ++n;
    
      puts ( "\nManual loop says:" );
      printf ( "%d representation bits\n", n );
    
      return 0;
    }
    Keeping to my principles, I felt the need to post what I think is a viable solution. But as a courtesy, I waited until you posted yours first, since you broached the subject.

    Cheers!

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Some others from "Part II":
    http://www.daniweb.com/code/snippet149.html

    And when working with bits, I avoid signed types.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. Binary representation of numbers...
    By roc in forum C++ Programming
    Replies: 2
    Last Post: 05-14-2003, 07:42 PM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM