Solving Programming Doubts: Logic, Shifts & Situations

  • Thread starter Thread starter rama1001
  • Start date Start date
  • Tags Tags
    Doubt Programming
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
5 replies · 2K views
rama1001
Messages
130
Reaction score
1
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.
 
Physics news on Phys.org
A good example of when to use AND and OR would be IF statements:

Check if string equals one of two values and if so do something:
Code:
if str == "alpha" or str == "bravo":
[INDENT]*do something*[/INDENT]

Check if string is not equal to both values and if not, do something:
Code:
if str <> "alpha" and str <> "bravo":
[INDENT]*do something*[/INDENT]

EDIT: Above examples are in Python.
 
Last edited:
jarednjames said:
A good example of when to use AND and OR would be IF statements:

Check if string equals one of two values and if so do something:
Code:
if str == "alpha" or str == "bravo":
[INDENT]*do something*[/INDENT]
Note that this won't work with C-type strings. Here's an example.
Code:
char str[] = "alpha";
if (str == "alpha" or str == "bravo")
{
   // won't execute
}

The body of the if structure won't execute because of the way C and C++ evaluate the name of a char array or a char pointer - as an address. Since str evaluates as an address, this address won't be equal to the addess of the string literal "alpha" or the string literal "bravo", so the if condition won't evaluate to true.
jarednjames said:
Check if string is not equal to both values and if not, do something:
Code:
if str <> "alpha" and str <> "bravo":
[INDENT]*do something*[/INDENT]
 
Mark44 said:
Note that this won't work with C-type strings. Here's an example.

The body of the if structure won't execute because of the way C and C++ evaluate the name of a char array or a char pointer - as an address. Since str evaluates as an address, this address won't be equal to the addess of the string literal "alpha" or the string literal "bravo", so the if condition won't evaluate to true.

Sorry, didn't know which language we were discussing so I just defaulted to python as the example. I'll note this in my above post.

For the purpose of demonstration though, they do the job for when to use AND and OR.
 
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.