About

I am a software developer in Seattle, building a new AI software company.

Ads

August 2009

Sun Mon Tue Wed Thu Fri Sat
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          

Ads


« XAML -- Almost A Full Blown Language | Main | Back from Vacation, Facing Heavy Traffic »

November 08, 2003

Comments

leppie

int BitCount(int v)
{
int count = 0;
while ( v != 0 )
v &= v-1;
return count;
}

count will never increase, perhaps a bit to minimal. A better a approach.

int BitCount(int v)
{
int count = 0;
while ( v = v >> 1) count++;
return count;
}

Wesner Moise

Your version of BitCount fails to work due to a common mistake. It is not a BitCount function that you wrote, but rather a function similar to Log to base 2.

For example, 0x80000000 should return 1, but your function returns 31.

With bit shifting, you need to write it like this:


int BitCount(int v)
{
int count = 0;
while ( v != 0)
{
if (v & 1) count++;
v >>= 1;
}
return count;
}

leppie

Thanks for pointing it out. I had my mind fixated "getting" the minimum bits for the value rather :)

Leon Bambrick

Hi Wesner, i have nothing intelligent to say like those other people, sorry.

just wanted to be picky and point out that the scientist in independence day wasn't Data from star trek ng, but jeff goldblum, from Jurassic Park and the Fly... he is also seen in the background of Annie Hall, as a flakey LA party guest putting a call through to his guru, because he'd 'forgotten his mantra'.

off topic? ok, i admit it.

B. Garvelink

I'm sorry Leon, but you're confused. Brent Spiner WAS in Independence Day and his character WAS (far) more fearful of daylight than Goldblum's.

Halfway across this page are some images.


:P

Hugh Brown

Wesner Moise:
The trick is that this algorithm strips off the bits, starting at the right. Take 7 (111b) as an example:
v = 111
v-1= 110
-> 110

v = 110
v-1= 101
-> = 100

v = 100
v-1= 011
-> = 000

So in three iterations, we have found that 7 has three bits set.

I believe that with a class definition like this:
class LinkedList
{
public:
int v;
LinkedList *next;
};

you could abbreviate your linked list code to this:

void Delete(LinkedList **list, int v)
{
while (LinkedList *tmp = *list)
{
if (tmp->v == v)
{
*list = tmp->next;
delete tmp;
return;
}
list = &tmp->next;
}
}

However, I may be misunderstanding the part about the double list.

I didn't see Independence Day but it looks like both Brent Spiner and Jeff Goldblum were in the film [1]. Based on the cast list, I'd say that Brent Spiner was the scientist (Dr. Brackish Okun). Who knew that Brent Spiner was so old? [2]

[1] http://us.imdb.com/title/tt0116629/
[2] http://us.imdb.com/name/nm0000653/

Hugh Brown

I think you can change this:
while (min0) min = mid+1;
else return mid;
}
to this:
while (min<=max)
{
int mid = (max+min)/2;
int cmp = v - array[mid];
if (cmp <= 0) max = mid;
else min = mid+1;
}
if (min < length && array[min] == v) return min;
else return -1;

and you pick up lowest matching value in the range. I think that's right but I have not tested the code.

Also, for reasons I can't quite remmeber but are related to integer overflow, it is recommended you write this:
int mid = (max+min)/2;
like this:
int mid = min + (max-min)/2;

If you like this kind of thing, check out this binary search coding challenge:
http://www.cix.co.uk/~solipsys/b_search/spec.htm

Wesner Moise

That's a more elegant approach, i agree.

The comments to this entry are closed.