actually it works correctly, since an int has 31 binary digits.Quote:

Originally Posted byDaved

Printable View

- 12-19-2006indigo0086Quote:

Originally Posted by**Daved**

- 12-19-2006hk_mp5kpdw
According to the table in my copy of Josuttis's The C++ Standard Library (7th printing, page 61):

*digits*: number of nonsigned bits (binary digits)

*digits10*: Number of decimal digits

Yeah, it is misleading, not what I'd expect when I think "digits" in my mind.

The related/relevant C constants (CHAR_BIT, etc...) are defined in one of the <climits>, <limits.h>, <cfloat>, or <float.h> headers (bottom of page 60). - 12-19-2006Daved
My mistake, I was confusing digits and digits10.

- 12-23-2006dnysveen
THanks for your replies. Can somebody give me a link of an explenation of &0xf and &0x1? Are there other &0x commands? Does &0x1 give you the first bit? I was never taught any bit manipulation in school and now I'm trying to get some basics down before my January interviews.

- 12-23-2006laserlight
I did a search of "bit manipulation C++" in a popular search engine... and the first result was an article from cprogramming.com's FAQ. Go figure :D

- 12-23-2006vart
& is bit and operator - read about bit-wise operations like http://msdn2.microsoft.com/en-us/library/17zwb64t.aspx

0xf, 0x1 etc is a hex number 0x prefix shows that the number is in the hex

0,1,2,3,4,5,6,7,8,9,a,b,c,d,f is regular hex digits - 12-23-2006DavidP
Before you learn what they do in things like bit manipulation, it would be good to learn what they actually are.

They are simply hexidecimal numbers, and nothing more.

Search on Google or Wikipedia for "hexidecimal" and learn how to count and do math in hexidecimal notation. "0x" is simply a prefix telling the compiler that the text following it will be a hexidecimal number. Then everything following the "0x" is a hexidecimal number.

After you learn about hexidecimal, you will be prepared to see what usefulness it has in working with and manipulating the bits of any variable.

[Edit]

A link: http://en.wikipedia.org/wiki/Hexidecimal

[/Edit] - 12-23-2006dnysveen
I already know all about hex and binary just not what the syntax of the operators are.

- 12-23-2006laserlightQuote:

I already know all about hex and binary just not what the syntax of the operators are.

- 12-23-2006vart
syntax is as for any other binary operator:

a = b+c;

a = b&c; - 12-23-2006dnysveen
I came up with another solution to the original question. Does anybody see anything wrong with my solution?

Code:`int countBits(int x){`

int numBits=0;

int intSize=sizeof(int)*4;

for(int i=0;i<intSize;i++){

if(x &0x1 == 1)

numBits++;

x>>=1;

}

return numBits;

}

- 12-23-2006CornedBee
Nothing wrong. It's the most straight-forward solution.

- 12-23-2006vart
Why

int intSize=sizeof(int)*4;?

Shouldn't it be

int intSize=sizeof(int)*CHAR_BITS;

Where 4 is coming from? in most platforms number of bits in char is 8 (and is defined by the constant in the <limits.h> - 12-23-2006dwksQuote:

[CHAR_BITS] is defined [. . .] in <limits.h>

Also, I would use sizeof(x), not sizeof(int), in case you change the type of the argument. - 12-23-2006dnysveen
I should have multiplied by 8 and not 4, my fault.