Thread: Dec to Bin

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by devour89
    my only question left is can I seperate all the '0' and '1' into an array?
    Array of characters, or array of integers 1s and 0s? One way perhaps (for a character array), use the bitset to_string() member function along with the string c_str() member function to get a c-style character string representation of the bitset that you can then strcpy into a temp array.

    Code:
    #include <cstring>
    #include <bitset>
    #include <iostream>
    #include <string> // Not sure if needed in this instance, just in case
    
    int main()
    {
        std::bitset<8> bits(218);
        char array[9];  // Extra space to hold NULL copied during strcpy
    
        strcpy(array,bits.to_string().c_str());
    
        std::cout << array << std::endl;
    
        return 0;
    }
    Again, should output: 11011010

    Edit: Forgot to mention that you can use the array indexing operator [] to access individual bits from the bitset in an array like manner which can be used in a loop to initialize an array. I.e. for the bitset in the above example we have:

    bits[0] = 0
    bits[1] = 1
    bits[2] = 0
    bits[3] = 1
    bits[4] = 1
    bits[5] = 0
    bits[6] = 1
    bits[7] = 1

    Using that principal might be useful for you.
    Last edited by hk_mp5kpdw; 10-04-2004 at 01:18 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Without using bitsets you could do this:
    Code:
    int input;
    //get input
    int x = input;
    int array[10];
    int i = 0;
    while(x > 0)
    {
      array[i++] = x % 2;
      x/2;
    }
    That will put the binary value of input in array in "backward" order where the power of 2 is the index of the element. For example:

    input = 26 would come out as 01011 if printed in ascending order of index, and 11010 if printed in descending order of index---which could be readily accomplished.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    120

    The int values of '1' and '0's...

    To get workable values from the char array, in a loop subtract the ascii value of '0' (0x30). '1' is 0x31...
    Or, instead of coverting to a string, in a loop, use a bitmask like this...
    Code:
    ...
            std::bitset<8> bits(218);
    	int mask = 1;
    	unsigned long bitArray[8] = {0};
    	unsigned long bl = bits.to_ulong(); 
    	for (int i = 7; i >=0 /*bad magic # :) */; i--, mask = mask << 1)
    	{
    		bitArray[i] = (bl & mask) >> (7 - i);
    		printf("%d", bitArray[i]);
    	}
    	printf ("\n");
    	return 0;
    Or if you want to keep everything it bitset, create the bitArray as type bitset[8] and then get rid of the extra ulong bl. Just & the mask against bits but keep everything else the same. This will put the LSB in index 0.

    PK

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    Elad, x/2 needs to be x/=2...you've got yourself a nice inifinte loop there

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  2. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  3. dec -> bin -> hex in arrays
    By chris1985 in forum C Programming
    Replies: 2
    Last Post: 05-09-2005, 08:15 AM
  4. Independent-Robert Fisk-Osama
    By a muslim in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-18-2001, 08:41 PM