Display from binary

Hi,

I would like to know if there is a way to display a character from a binary value just like we do from the decimal value or hexadecimal value:

1
2
char c = 0x64;
cout << c;


Thank you.
you can't write binary numbers in C++. Fortunately hexadecimal is used for this very reason!

A 32 bit number in binary can be very hard to read:
00111101101111001010000111101001

If you want to know what bit 25 is, you really have to put a pencil on the screen and count each digit. A hexadecimal digit represents a set of 4 binary numbers. IN this case, the number above looks like this:
0x3dbca1e9

It's still looks a little garbled, but with some practice you can get to understand it better. In this case, if I want to know what bit 25 is, I think that is the first bit in the 7th letter. Why? Because 4bits per letter * 6 letters =24 bits. Then one more bit into the 7th letter is our bit. The 7th letter is D, which is 1101, therfore the 25th bit is 1.

Get to know your hex<-->bin conversions (there are only 16) and you'll never want to look at binary again. It's awful!

For your case you have 0x64, 6 is 0110 and 4 is 0100. So 0x64 is 01100100
Last edited on
C++11 user-defined literal:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
template< char FIRST, char... REST > struct binary
{
    static_assert( FIRST == '0' || FIRST == '1', "invalid binary digit" ) ;
    enum { value = ( ( FIRST - '0' ) << sizeof...(REST) ) + binary<REST...>::value  } ;
};

template<> struct binary<'0'> { enum { value = 0 } ; };
template<> struct binary<'1'> { enum { value = 1 } ; };

template<  char... LITERAL > inline
constexpr unsigned int operator "" _b() { return binary<LITERAL...>::value ; }

template<  char... LITERAL > inline
constexpr unsigned int operator "" _B() { return binary<LITERAL...>::value ; }

#include <iostream>

int main()
{
     char cstr[] = { 1100010_b, 1101001_B, 1101110_b, 1100001_B, 1110010_B, 1111001_B, 0 };

     std::cout << cstr << '\n' ;
}

http://coliru.stacked-crooked.com/a/c5942ed7f025ea3c



C++14 binary literal: ( -std=c++1y with GCC 4.9 )

1
2
3
4
5
6
7
8
#include <iostream>

int main()
{
     char cstr[] = { 0b1100010, 0B1101001, 0b1101110, 0B1100001, 0b1110010, 0b1111001, 0 };

     std::cout << cstr << '\n' ; 
}

It feels like just yesterday C++0x wasn't assigned a year. We have C++14 already? But compilers are just starting to get up-to-date with C++11!

I think adding binary literals will make things easier for beginners, but then also just promote bad practices. In what world would you ever want to read binary over hex? Allowing people to code in binary will just make life harder for those of us who use hex when we review those people's code.
Last edited on
> In what world would you ever want to read binary over hex?

In this world, when I want to deal with bits in groups that are not a multiple of four. For instance, to test a std::bitset<5> for a specific combination of bits.
Thank you for your answer.
JLBorges, I am working on a code for writing PBM image files in binary mode. In those files, 1 means black and 0 means white, so, to write for instance a line of three colors 101, you need to write the binary value of 10100000 which is the character 0xA0. For a line of 01000000, you will write the character 0x64.

If I already know the binary scheme of a character, I would like to use it directly instead of looking for the hex value.

Thanks!
Topic archived. No new replies allowed.