Is it possible to denote a value as binary?
For example, hex is denoted like so:
[code]
var = 0xFF
[code]
Is there a way to do this for binary values?
Thanks!
Is it possible to denote a value as binary?
For example, hex is denoted like so:
[code]
var = 0xFF
[code]
Is there a way to do this for binary values?
Thanks!
Indeed, working in hexadecimal is working in binary. It's just more compact. (The same could be said of octal...).
The other solution is to use the strtoul standard function (declared in stdlib.h), for example. That would looks like
Code:int main() { unsigned int i = strtoul("11010101110", NULL, 2); ... }
I hate real numbers.
A better way is to use your calculator to convert your binary number into hex, or alternatively, if the calculator does not support such a function, find something on the web that does.
C does not offer many alternative ways. Doing it at runtime wastes cpu cycles, but it isn't terrible.
I remember being surprised when I found out that you couldn't "directly" use binary in C or C++.
Most people who work with binary use hex in their programs. (Of, course, the computer doesn't care... Everything is in binary inside the computer... By default, numbers are converted to/from decimal for us during input/output and at compile time.)
You can fairly easily learn to convert between binary and hex in your head, because each "group" of 4 bits, converts exactly to one hex-digit. You only have to learn 16 conversions and you can convert numbers of any size! (It's much easier than converting between binary and decimal, and you already know zero and one, so you only need to learn 14 more.)
For example...
0101 (binary) = 5 (hex) = 5 (decimal)
0101 0101 (binary) = 55 (hex) = 85 (decimal)
0101 0101 0101 0101 = 5555 (hex) = 21,845 (decimal)
And when we do write-out binary numbers, we often put a space between each group of 4 characters as I have done above.
Hex is much easier (than binary) to read, write, and speak, especially when you go beyond 8 bits.