Is putting in binary as a value possible? I want something like char test = 00101011
and it will become 43. I know this is possible by making a function that converts binary to decimal (which can be inputted) but thats not direct and Im pretty sure it takes time.
Asked
Active
Viewed 44 times
0

Bfyuvf
- 139
- 9
-
1Have a look [here](https://stackoverflow.com/questions/2611764/can-i-use-a-binary-literal-in-c-or-c). – PhilMasteG Jun 02 '22 at 10:44
1 Answers
0
You need to put the prefix 0b
.
#include <iostream>
int main()
{
char c = 0b00101011;
std::cout << static_cast<int>(c) << std::endl;
}

kiner_shah
- 3,939
- 7
- 23
- 37
-
`0b0010'1011` can also use the apostrophe as a number grouping, to make the long number sequences more legible. – Eljay Jun 02 '22 at 14:58
-