rama1001 said:
I have been working on programming and trying to learn fast all the things. I have some doubts about following logics that when to use and where. Please explain me in short and undestandble way.
1) when to use exactly AND, OR and reamining logic functions.
2) when to use arthimatic and logic right lest shifts.
Its better if some explain me with sistuations.
Thank you all.
1) When it comes to logical operators you either use the binary version, or you use a version that is type dependent.
The binary version is like it sounds, it takes two bits of data and performs the operation bit-wise on every bit of the data. Using your standard tables for AND,OR,XOR you just apply that in a binary way by taking your input bits and producing an output bits and then storing the result somewhere.
The other type is the kind that you find with either features of a language (like BASIC) or with operator overloading (in C++) or perhaps in some extension or exotic platform. When this is the case, the operation is specified by the implementation and is going to be platform dependent according to the specification.
Note that the binary version works on any form of data where the two inputs have exactly the same size.
2) Shifts and Logic
I don't completely understand what you're saying about arithmetic but shifts are used for a variety of purposes.
Shifts are used for a variety of reasons, but one reason is to simulate multiplication and division. When you shift right you are dividing by two and when you shift left you are multiplying by two.
Word of warning: In the division case you are doing what is called an integer divide. This basically divides by two and gets rid of the fractional part if one existed. The multiply will always produce an integer, but if there is an overflow of some sort, the data will not reflect the true value.
Another common feature of using ANDs and shifts is bit extraction. Data can be a scarce commodity and you typically want to represent data in a compact form which means custom allocation of bits where you use the minimum amount of bits for your purpose.
Using ANDs and shifts in a loop helps you extract these bits very fast and then you can take those bits and do what you want with them.
There are other uses but these are some common ones.