Forums

ForumsProgramming Forum

Use of Binary

Posted Sep 21, '11 at 12:12pm

Somewhat49

Somewhat49

1,669 posts

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?

 

Posted Sep 21, '11 at 7:03pm

master565

master565

4,288 posts

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)

 

Posted Sep 21, '11 at 8:08pm

Carlytoon

Carlytoon

329 posts

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.

 

Posted Sep 21, '11 at 9:14pm

rubixcuber

rubixcuber

1 post

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.

 

Posted Sep 21, '11 at 10:44pm

Darkroot

Darkroot

2,879 posts

@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.

 

Posted Sep 29, '11 at 3:14pm

Somewhat49

Somewhat49

1,669 posts

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.
 
Reply to Use of Binary

You must be logged in to post a reply!