|
-
March 31st, 2007, 03:16 AM
#1
[RESOLVED] Are Binary Operators Used Alot in Modern Programming
I'm wondering if they are because they are sort of confusing and I'm not sure if I need to learn them or not...
Just to make sure, the only binary operators are &(and), |(or) and ^(exclusive or), right?
Edit: I do know that <variable> & 1 will return true if variable is odd.. but that's all. :P
Last edited by Mybowlcut; March 31st, 2007 at 03:21 AM.
-
March 31st, 2007, 03:47 AM
#2
Re: Are Binary Operators Used Alot in Modern Programming
I think you are confusing binary operators with Bitwise operators.
Binary operators are operators which take 2 arguments, usually left and right arguments, for example: =, <<, >>, %, ...
Bitwise operators are operators which operate on the bits of a variabe. Ofcourse, some bitwise operators can also be binary operators.
For example, operator &:
Consider the binary representation of a short, it has 8 bits. For instance, the number 7 will be represented as 00000111.
So you may ask what does the meaning of the code above. Well, the bitwise operator &, operates as an AND operation on each of the bits. The result bit will be 1 if and only if the 2 arguments matching bit is also 1:
Code:
7 (Decimal) = 00000111 (Binary)
&
1 (Decimal) = 00000001 (Binary)
------------
res = 00000001 (Binary) = 1 (Decimal)
You can also check this link for more references. About your question, if they are used alot - well it depends on the application and the requirements, but they can speed up calculations plenty of times and I have seen these operators put to commercial use plenty of times.
Last edited by Zachm; March 31st, 2007 at 03:54 AM.
-
March 31st, 2007, 04:01 AM
#3
Re: Are Binary Operators Used Alot in Modern Programming
Ok... I've read it. It seems like a pretty low-level sort of operator. So to resolve my question, is it used alot? I'd think that it'd be used for programming that dealt intensely with memory? Or maybe something... weird... haha. I have two questions actually...
Is it used alot? And:
is it used in games?
-
March 31st, 2007, 04:12 AM
#4
Re: Are Binary Operators Used Alot in Modern Programming
I have already answered (edited my last post).
As I wrote, it can speed up certain calculations, therefore an application that needs to run with a high frame-rate (like games) may want to use these operators.
They are low level and the outcome is not always easy to understand, therefore if they are not needed (when performance is not an issue), I'd recommend on using regular arithmetic operators that are more readable and easy to understand.
-
March 31st, 2007, 04:18 AM
#5
Re: Are Binary Operators Used Alot in Modern Programming
They're used a lot when interacting with the Win32 API, or the POSIX API, or a number of other API, to manipulate bit fields and masks.
For example, have a look at the CreateWindowEx Win32 function.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
March 31st, 2007, 04:23 AM
#6
Re: Are Binary Operators Used Alot in Modern Programming
I just bought a GamesTutorial thingy which I think Zach suggested... and I saw that the Win32 API can be used to make 2d games... Binary stuff scares me though... Thanks guys.
-
March 31st, 2007, 07:13 AM
#7
Re: [RESOLVED] Are Binary Operators Used Alot in Modern Programming
 Originally Posted by Mybowlcut
I'm wondering if they are because they are sort of confusing and I'm not sure if I need to learn them or not...
Let's say you have 16 yes/no, true/false, variables, i.e. variables that can only have two possible values. You have a choice of declaring 16 int variables where each one is 1 or 0, or declare a single integer and just use each bit to denote each of the 16 item's state.
The former needs 16 ints, the latter needs just 1 int. To access each bit, you use bitwise operations.
Let's take a simple example:
Person data:
Male / Female:
Married / Single
Employed / Non-Employed
Citizen / Non-Citizen
You have these 4 items. You can declare 4 int or bool type variables to denote each item, or you can declare one integer for everything. If we want to declare just one int, you have to come up with what each bit denotes.
Bit 0 - gender (0 - male, 1 - female)
Bit 1 - marital status (0 - not married, 1 - married)
Bit 2 - employment status (0 - not employed, 1 - employed)
Bit 3 - citizen status (0 - non-citizen, 1-citizen)
And we have an integer encoded like this to denote a particular person's data:
1011
If bits are counted starting from the right going left, and the first bit is 0, the above means, female, married, not employed, and is a citizen.
The trick now is how do you access each item programatically? You use bitwise operations:
Code:
int status;
//...
status & 1; // gender
status & 2; // marital status
status & 4; // employment
status & 8; // citizenship
Code:
if ( status & 4 ) { /* person is employed */ }
The reason the values are 1, 2, 4, and 8 is the following:
0001
0010
0100
1000
The bit pattern above denotes 1, 2, 4, and 8. If you do a bitwise & with 4, you get either 0, or you get a 4. If you get 4, then this means the bit was "on", and that means the person was employed.
This is not just a programming thing. Ever see dip switches on your circuit board, or even in your favorite appliances? Why is there this one dip switch with multiple little switches on it? Why not, say, 8 separate switches instead of this one dip with 8 toggles on it? It's the same principle.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; April 6th, 2007 at 12:27 PM.
-
March 31st, 2007, 02:44 PM
#8
Re: [RESOLVED] Are Binary Operators Used Alot in Modern Programming
MY opinion on this is: Learn them, whats the harm?
I've use these several times in network and game programming and in encryption, its pretty useful to know. Also , It will look confusing at first but once you understand what it is doing and get the syntax behind it it makes good since and will even look good.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|