Number to bit vector


 
Thread Tools Search this Thread
Top Forums Programming Number to bit vector
# 1  
Old 02-22-2016
Number to bit vector

Is there a function to convert number (unsigned int for this example) to binary? I could not find a simple one thru google.
While I was learning bloom filter with the example, I was wondering if anybody can help me to
1) display the real bits vector for the bloomfilter;
2) if dataset is very big >1 trillion (1.0E12) or more, how to decide the FILTER_SIZE and FILTER_SIZE_BYTES... (all the #define ) to make sure overflow will NOT happen for bloomfilter (variable filter[] in the code).
For 1) it is Line 172 in the code, which print out the integers ---that's one of the reasons I want to see the actually bits vector for each word (from /usr/share/dict/american-english, in my box Linux 3.19.0-32-generic #37~14.04.1-Ubuntu ).
Thanks a lot!

---------- Post updated at 04:28 PM ---------- Previous update was at 04:24 PM ----------

Code:
/*******************************************************************************
* The code has been replaced with attachment
* bloom_filter01.txt -- A simple bloom filter implementation
*******************************************************************************/


Last edited by yifangt; 03-09-2016 at 03:31 PM.. Reason: Try attachment
# 2  
Old 02-22-2016
I used this function for ages, don't even remember its origin:

Code:
char *conv_to_zeroes_ones(void *in)
{
  register int  i, pos;
  unsigned int  *input;
  char          bit;
  char          buf[256];
         input = (unsigned int *)in;
        memset(buf, 0, 256);
        pos = 0;
         for(i = 31; i >= 0; i--)
        {
                if(((*input >> i) & 1))
                        bit = '1';
                else
                        bit = '0';
                buf[pos++] = bit;
                if((i > 0) && (i % 4) == 0)
                {
                        if((i % 8) == 0)
                        {
                                buf[pos++] = ' ';
                                buf[pos++] = ':';
                                buf[pos++] = ' ';
                        }
                        else
                        {
                                buf[pos++] = ' ';
                        }
                }
        }
        return buf;
}

the result looks like this:

Code:
 
 $ bits 1234
    1234 = 0000 0000 : 0000 0000 : 0000 0100 : 1101 0010

This User Gave Thanks to migurus For This Post:
# 3  
Old 02-22-2016
Thanks migurus!
That's almost what I was looking for, but I expected
Code:
$ bits 1234
1234 = 00000000000000000000010011010010    //for 32 bits

Or,
Code:
$ bits 1234
1234 = 0000000000000000000000000000000000000000000000000000010011010010    //for 64 bits

One I found from stackoverflow is:
Code:
void binary(unsigned n)
{
    unsigned i;
    for (i = 1u << 32; i > 0; i = i / 2)
        printf("%d", (n & i)? 1 : 0 );
}

Thanks again!

Last edited by yifangt; 02-29-2016 at 01:20 PM..
# 4  
Old 02-25-2016
The message has been posted as a different question. Sorry for the confusion!

Last edited by yifangt; 02-29-2016 at 01:17 PM.. Reason: Highlight clues
# 5  
Old 02-26-2016
You can convert decimal to binary using bc...there is no need to write a special C program...
Code:
echo "obase=2;1234" | bc

These 2 Users Gave Thanks to shamrock For This Post:
# 6  
Old 02-29-2016
Hi Shamrock!
I was looking for a function which can be integrated into my code. Thanks for your input, though!
# 7  
Old 02-29-2016
Quote:
Originally Posted by yifangt
Hi Shamrock!
I was looking for a function which can be integrated into my code. Thanks for your input, though!
Hi yifangt,
You are aware that you can access shell scripts from C code using the system() function, aren't you?
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Windows & DOS: Issues & Discussions

Which version of Windows Vista to install with a product key? 32-bit or 64-bit?

Hello everyone. I bought a dell laptop (XPS M1330) online which came without a hard drive. There is a Windows Vista Ultimate OEMAct sticker with product key at the bottom case. I checked dell website (here) for this model and it says this model supports both 32 and 64-bit version of Windows... (4 Replies)
Discussion started by: milhan
4 Replies

2. Programming

How bit representation of a number can be done in c?

I have an assignment in which a character is the input of which some bits(from a position to certain position) are to be inverted (1's complement) and then the resultant character is to be returned....for example unsigned char x = J from p = 3 to offset n = 5 01001010 inverted to... (1 Reply)
Discussion started by: ezee
1 Replies

3. Shell Programming and Scripting

How to handle 64 bit arithmetic operation at 32 bit compiled perl interpreter?H

Hi, Here is the issue. From the program snippet I have Base: 0x1800000000, Size: 0x3FFE7FFFFFFFF which are of 40 and 56 bits. SO I used use bignum to do the math but summing them up I always failed having correct result. perl interpreter info, perl, v5.8.8 built for... (0 Replies)
Discussion started by: rrd1986
0 Replies

4. Programming

Help with bit vector and setting its bits?

void shuffle (card_t cards, card_t cards2, char *bitvec) { int i; unsigned char temp, r; srand(time(NULL)); for (i = 0; i < 52; i++) { r = rand() % 52; temp = bitvec; if (!(temp & (1 << (8- (r % 8))))) { temp |= (1 << (8- (r % 8)));------->is this my problem? } } } ok so I am... (1 Reply)
Discussion started by: bjhum33
1 Replies

5. Shell Programming and Scripting

Help needed in converting number to bit

Hi, I have the following file input:- 542 4795 61 543 4795 61 544 0 0 545 292 ... (5 Replies)
Discussion started by: ahjiefreak
5 Replies

6. Programming

copying or concatinating string from 1st bit, leaving 0th bit

Hello, If i have 2 strings str1 and str2, i would like to copy/concatenate str2 to str1, from 1st bit leaving the 0th bit. How do i do it? (2 Replies)
Discussion started by: jazz
2 Replies
Login or Register to Ask a Question