Help with bit vector and setting its bits?


 
Thread Tools Search this Thread
Top Forums Programming Help with bit vector and setting its bits?
# 1  
Old 04-17-2012
Help with bit vector and setting its bits?

Code:
 
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[r/8];
if (!(temp & (1 << (8- (r % 8)))))
{
temp |= (1 << (8- (r % 8)));------->is this my problem?
}
}
}


ok so I am trying to use a bit vector to keep track of some random numbers and I suppose I just dont understand how to use bit operations verry well since i thought that this code would work but from the results i have been getting the bit just stays 0 no matter what. So i want to check and see if the bit is 0 and if it is i want to set it to one but either my check or set of the bit isnt wokring. if anyone would please tell me what im doing wrong i would appreciate it.
# 2  
Old 04-17-2012
There is no problem with your bitwise operations. Your pointers on the other hand ...

Do you expect changing temp to change bitvec? That'd require a pointer. You're not using one. Also, there is no point checking if the bit is unset if you're going to set it. If it's unset and you set it is the same as if it's set and you set it... Make sense?

So throwing out the temp variable (or using pointers) and throwing away the if condition, your code works here:

Code:
        for (i = 0; i < 52; i++)
        {
                r = rand() % 52;
                printf("round %d: r=%d bitvec[%d]=%d bit=%d ",
                        i, r, r/8, bitvec[r/8], 8 - (r % 8));
                bitvec[r/8] |= (1 << (8 - (r % 8)));
                printf("bitvec now=%d\n", bitvec[r/8]);
        }

Login or Register to Ask a Question

Previous Thread | Next Thread

7 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

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... (11 Replies)
Discussion started by: yifangt
11 Replies

3. What is on Your Mind?

Place Bits & Win Bits!!! - 17th Annual Satellite Awards

Ten movies have been nominated as best motion picture by the International Press Academy, presentation of the 2012 Satellite Awards will be held on 16th December at Los Angeles, CA. Place your bits here on one of the below nominated movie of your choice:- Argo ... (0 Replies)
Discussion started by: Yoda
0 Replies

4. 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

5. UNIX for Dummies Questions & Answers

32 bits procesaor with 64 bits Solaris

people i have a problem i have a 32 bits sparc processor, and solaris 64 bits processor, i install a oracle data base 64 bits, but my oracle will not run because my processor is from 32 bits this is ok??, i know if i have x86 i cannot install a 64 bits operatin system in a 32 bits processor. ... (0 Replies)
Discussion started by: enkei17
0 Replies

6. UNIX for Dummies Questions & Answers

Setting Last-Modified Bit in Apache

Hi, I have a co-worker that uses javascript code to generate the last-modified date on a file. The problem is our server does not properly send this date so javascript can display it. I know that it is better to use server-side scripting to generate this but she has already put it on multiple... (1 Reply)
Discussion started by: robbieg
1 Replies

7. UNIX for Dummies Questions & Answers

Changing 24 bits to 8 bits display

Hello all, I was wondering if anyone can tell me how to change 24 bits depth display to 8 bits depth display for Sun Ultra1, running Solaris 8? THANKS in advance. I think that the command is ffbconfig, but it has nothing about depth. (4 Replies)
Discussion started by: larry
4 Replies
Login or Register to Ask a Question