Thread: How To Get The Binary Representation Of A Number Without Using Division

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    13

    Lightbulb How To Get The Binary Representation Of A Number Without Using Division

    Hello All,

    how can we print the binary bit representation of a number without division.
    there is a way probably we can do it with the use of UNION.

    any suggestions ?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Yes, you can do it using a union. There are other ways, as well. Is this a quiz or what?

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    13

    Wink

    hello MacGyver,

    i need the procedure of doing that as that is required for one of my presentations. it would be great if you could share the two ways.

    and ya its not a quiz NEO.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I'm not entirely sure how portable it is, but the idea is to have a union of a struct and an integer type of variable. The struct should contain bitfields representing each bit in the variable. You then need to define a function to print out each bit individually.

    Portability issues:

    • I'm not entirely sure if the bitfields will be properly lined up with no spacing. There was possibly a discussion on this recently, but I'm not entirely sure what was concluded, although I believe this may not be an issue.
    • Endianess. This basically means you may have to print all the bits "backwards" on some machines in order for it to make sense. This requires you to know which type of machine you plan on doing this type of code on or attempting to detect the endianess of the machine you are compiling your code on.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    IF I understand correctly, why not plain old binary operators?
    Code:
    int i, j = 0;
    int mask = 0x01;
    int var = 0xFFFFFFFF;
    for (; i < sizeof(var); i++)
    {
        for (; j < 8; j++, mask <<= 1)
        {
            if (var & mask)
                printf("1");
        }
        printf(" ");
    }
    printf("\n");
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You can also use subtraction to do the same work as division.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    13
    thanks all for all your valuable inputs

    THANTOS:

    how exactly can we get the desired result using subtraction ?

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well you have to go back to the defination of multiplication: 5 * x = x + x + x + x + x

    So say I have 16 and I want to divide it by 5 but I can't use division directly. I can then do something like:
    Code:
    q := 0
    r := 0
    x := 16
    d := 5
    while x >= d do
      q := q + 1
      x := x - d
    end while
    r := x
    Now q holds 3 and r holds 1. So 16 / 5 is 3 remainder of 1.

    If x is negative then you can do the same process by adding instead of subtracting and making a few small changes.

  9. #9
    Registered User
    Join Date
    Jun 2008
    Posts
    13
    Thanks Thantos !!!


    can anyone help me out by suggesting a way to do the same using Unions?

    thanks in advance ..

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    MacGyver already has.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    i was just thinking whether even this one is correct??

    Code:
     
    #include <stdio.h>
    void convert(int n)
    {
     int mask=0X000000001;
     if(n)
     {
    	convert(n>>1);
    	if(mask & n)
    		printf("1");
    	else
    		printf("0");
     }
    }
    
    int main()
    {
     int n=34;
     convert(n);
    return 0;
    }

  12. #12
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    Code:
    #include <iostream>
    
    
    
    int main()
    {
        int num = 0x1111;
        
        for(int i = sizeof(int)*8-1; i >= 0; i--)
        {
                std::cout<<(num>>i)%2;        
                
                
        }
        
        
        
        std::cin.ignore();
        std::cin.get();   
    }

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    This is the C forum, so try not to use cout and other C++ stuff if you can help it. (Or C# stuff for that matter. )

    BTW:
    Code:
    sizeof(int)*8
    ->
    Code:
    #include <limits.h>
    sizeof(int)*CHAR_BIT
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Quote Originally Posted by Elysia View Post
    IF I understand correctly, why not plain old binary operators?
    Code:
    int i, j = 0;
    int mask = 0x01;
    int var = 0xFFFFFFFF;
    for (; i < sizeof(var); i++)
    {
        for (; j < 8; j++, mask <<= 1)
        {
            if (var & mask)
                printf("1");
        }
        printf(" ");
    }
    printf("\n");
    Isn't this broken ? I mean, there's definitely missing a "else" statement. Plus, you should set j to 0 before entering the most inner-loop.
    I hate real numbers.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Most likely it is. I wrote it off the top of my head without checking it.
    Missing "else" and missing initialization for both i and j, I believe.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Nim Trainer
    By guesst in forum Game Programming
    Replies: 3
    Last Post: 05-04-2008, 04:11 PM
  3. Binary representation in C
    By @nthony in forum C Programming
    Replies: 25
    Last Post: 11-10-2007, 12:43 AM
  4. large binary number division
    By nappaji in forum C Programming
    Replies: 3
    Last Post: 08-08-2006, 01:54 PM
  5. large binary number division
    By nappaji in forum C++ Programming
    Replies: 1
    Last Post: 08-08-2006, 12:01 AM