Solving Programming Doubts: Logic, Shifts & Situations

In summary, AND operations are used to compare two sets of data, while OR operations are used when one of the sets of data must be chosen as the result. The third logical operator, XOR, is used when two sets of data must not be compared, but instead a result must be produced. Shifts are used for a variety of purposes, one of which is simulating multiplication and division. Finally, bit extraction is often done using ANDs and shifts in loops, which allows for quick and efficient representation of data.
  • #1
rama1001
132
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.
 
Technology news on Phys.org
  • #2
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:
  • #3
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]
 
  • #4
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.
 
  • #5
Right, the OP didn't specify a language, and yours was a good example of what he was asking for.
 
  • #6
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.
 

1. What is the best approach to solving programming doubts?

The best approach to solving programming doubts is to first identify the specific area or concept that you are struggling with. Then, research and review the relevant documentation, tutorials, or examples. It can also be helpful to seek out advice from more experienced programmers or participate in online forums or communities where you can ask questions and receive guidance.

2. How can logic be used to solve programming doubts?

Logic is a fundamental aspect of programming and can be used to solve doubts by breaking down complex problems into smaller, logical steps. By identifying the input, output, and necessary operations, you can create a logical flow to your code and troubleshoot any issues that arise more easily.

3. What are some common situations where programming doubts may arise?

Programming doubts can arise in a variety of situations, such as when trying to understand a new programming language, debugging code that is not working as expected, or trying to optimize performance. They can also occur when encountering unfamiliar programming concepts or trying to implement a complex algorithm.

4. How can shifts in thinking help to solve programming doubts?

When faced with a programming doubt, it can be helpful to take a step back and approach the problem from a different perspective. This shift in thinking can often reveal new insights or potential solutions. Additionally, trying out different approaches or methods can also help to overcome programming doubts.

5. Is it important to document and track programming doubts?

Yes, it is important to document and track programming doubts as they can serve as valuable learning experiences. By keeping a record of the doubts you encounter and how you ultimately solved them, you can refer back to them in the future and potentially help others who may have similar doubts. It can also help to identify areas where you may need to improve or further your understanding.

Similar threads

  • Programming and Computer Science
Replies
15
Views
1K
Replies
6
Views
652
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
22
Views
921
  • Programming and Computer Science
Replies
1
Views
729
  • Programming and Computer Science
2
Replies
41
Views
3K
  • Programming and Computer Science
2
Replies
49
Views
3K
  • STEM Academic Advising
Replies
2
Views
708
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
8
Views
878
Back
Top