Also depends how you gonna use your binary numbers. For performance i would use a 8-32 bits type and make some functions/macros to manipulate single bits. If you do alot of computations with your binary numbers and just want to manipulate single bits you dont wanna use a 'bitset'. Convert too and from binary string representation is as trivial as for a 'bitset'.
But you right about one thing that it would not be a great effort to have bin representation of numbers. Some C compilers, mostly for microcontroller processors actualy have a binary number representation like "int a = 00100101b;"
binary numbers
Quote:Original post by smurfkiller
8-32 bits type
Surely those depend on the architecture (which is nowadays and never was Intel x86): Fastest minimum width integer types. Also.
Quote:and make some functions/macros/templates, but avoid macros in C++
Rephrased for the C++ case ;)
Quote:Some C compilers, mostly for microcontroller processors actualy have a binary number representation like "int a = 00100101b;"
Indeed, but then that's not C++ standards conforming and afaik also not C standards conforming. But then, such low-level programming of course is allowed to fully utilize compiler extensions. Sidenote: The OS kernel Linux (low level) and afair BSD depend on tons of compiler extensions. Can't say anything about Windows, but I bet they do it, too.
In any case you can always just use hexadecimal literals =P One hexadecimal digit is the equivalent to four binary digits. You enter them like this in the case of C and C++: 0x1234 (that's like 0001 0010 0011 0100 in binary).
Quote:Original post by Sik_the_hedgehog
In any case you can always just use hexadecimal literals =P One hexadecimal digit is the equivalent to four binary digits. You enter them like this in the case of C and C++: 0x1234 (that's like 0001 0010 0011 0100 in binary).
Alternatively, you can also write octal numbers, where each digit represent three bits, yay. Format is basically 0[0-9]*, with usual integer postfixes allowed.
Quote:Original post by phresnel
Alternatively, you can also write octal numbers, where each digit represent three bits, yay. Format is basically 0[0-9]*, with usual integer postfixes allowed.
No. The format is 0[0-7]*. You'll get a compilation error if you use an 8 or a 9 in an octal expression.
Quote:Original post by alvaroQuote:Original post by phresnel
Alternatively, you can also write octal numbers, where each digit represent three bits, yay. Format is basically 0[0-9]*, with usual integer postfixes allowed.
No. The format is 0[0-7]*. You'll get a compilation error if you use an 8 or a 9 in an octal expression.
D'Oh, that failure came from my habit of writing [0-9] in regexen (read: I am very used to write that special sequence [0-9]), instead of using proper character groups and special signs.
Here's an interesting sidenote:
const float x = 0xFe9f; const float y = 07e9f; const double p = 0xFe9; const double q = 07e9; const long double a = 0xFe9L; const long double b = 07e9L;
See also this. Note that hexadecimal-floating-point-constants are C99, and if I looked right, C++0x won't have them (see page 25: "Floating Literals" in C++0x draft September 2008), though GCC supports them as an extension to C++.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement