ForumsProgramming ForumUse of Binary

5 2410
Somewhat49
offline
Somewhat49
1,632 posts
435

I just recently learned Binary for programming (C++) and now I can't figure out why I learned it, what's its use in programming? All I know about Binary is that the computers use it for calculating stuff, but how does that help in C++ when you can just use regular decimal?

  • 5 Replies
master565
offline
master565
4,125 posts
600

It doesn't? Why would you try to use binary in C++? Computers today don't even allow you to program in binary (ignoring using binary codes in some very high level languages)

Carlytoon
offline
Carlytoon
325 posts
235

Why did you learn binary? Must of the programming languages (and all that I know so far) dont use it, and if you gonna learn C++ as first language I sugest you to try a ligther language, C++ is really strict.

rubixcuber
offline
rubixcuber
1 posts
50

It can be useful to understand binary when coding, since data is all represented in binary and all operations are done on binary values. Computers don't work with data in decimal, and this can lead to some surprises if you don't understand how binary works.

One of the most direct uses of binary you may encounter in coding is in the implementation of flags, or simple true/false values. Often, instead of having a separate variable or boolean for each possible true/false option, the values will be combined via a larger data type. You can represent eight different true/false values with a single byte and then extract the value of each by ANDing the combined value with a 1 in the appropriate bit.

It can also be very important in overflow situations. If you are working with a char data type in c++, and have the value of 129, which is 10000001 in binary, if you double that you will get 2, because in binary you end up with 100000010, but a char can only hold 8 bits so you lose the leftmost 1 and get 00000010 or 10 which is 2.

In any case, understanding the mechanics of what's going on will help in the long run, and probably save on some headaches down the road.

Darkroot
offline
Darkroot
2,779 posts
1,120

@rubixcuber Yep pretty much covers it. In summary you have to understand what you're working with otherwise when something happens you don't like or something related specifically to the properties of having a binary system then you're lost.

Somewhat49
offline
Somewhat49
1,632 posts
435

It can be useful to understand binary when coding, since data is all represented in binary and all operations are done on binary values. Computers don't work with data in decimal, and this can lead to some surprises if you don't understand how binary works.
One of the most direct uses of binary you may encounter in coding is in the implementation of flags, or simple true/false values. Often, instead of having a separate variable or boolean for each possible true/false option, the values will be combined via a larger data type. You can represent eight different true/false values with a single byte and then extract the value of each by ANDing the combined value with a 1 in the appropriate bit.
It can also be very important in overflow situations. If you are working with a char data type in c++, and have the value of 129, which is 10000001 in binary, if you double that you will get 2, because in binary you end up with 100000010, but a char can only hold 8 bits so you lose the leftmost 1 and get 00000010 or 10 which is 2.
In any case, understanding the mechanics of what's going on will help in the long run, and probably save on some headaches down the road.

Thanks that really helped! I always wondered when in some situations I get overflow, even though the decimal number I put in is within it's size standards, but in binary it goes over. I also heard that coding using binary can help save space when doing big operations.
Showing 1-5 of 5