actually it works correctly, since an int has 31 binary digits.Originally Posted by Daved
actually it works correctly, since an int has 31 binary digits.Originally Posted by Daved
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).
"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
My mistake, I was confusing digits and digits10.
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.
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
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
& 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
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
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]
I already know all about hex and binary just not what the syntax of the operators are.
Read the FAQ. It will teach you about the syntax.I already know all about hex and binary just not what the syntax of the operators are.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
syntax is as for any other binary operator:
a = b+c;
a = b&c;
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
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; }
Nothing wrong. It's the most straight-forward solution.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
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>
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
You mean <climits>.[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.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
I should have multiplied by 8 and not 4, my fault.