Solving Programming Doubts: Logic, Shifts & Situations

  • Thread starter Thread starter rama1001
  • Start date Start date
  • Tags Tags
    Doubt Programming
Click For Summary

Discussion Overview

The discussion revolves around programming logic, specifically the use of logical operators (AND, OR) and bitwise shifts (arithmetic and logical). Participants seek clarification on when to apply these concepts in various programming situations.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants provide examples of using AND and OR in IF statements, noting differences in behavior across programming languages like Python and C/C++.
  • One participant emphasizes that logical operators can be binary or type-dependent, with the binary version applying bit-wise operations on data.
  • Another participant discusses the use of shifts for simulating multiplication and division, highlighting that right shifts divide by two and left shifts multiply by two.
  • Concerns are raised about integer division when using right shifts, as it discards any fractional part.
  • Participants mention the use of ANDs and shifts for bit extraction, particularly in scenarios where data representation needs to be compact.

Areas of Agreement / Disagreement

There is no consensus on the best practices for using logical operators and shifts, as participants present varying examples and explanations, leading to multiple competing views.

Contextual Notes

Some limitations include the dependence on specific programming languages and the potential for confusion regarding the behavior of logical operators in different contexts. The discussion also reflects varying levels of understanding among participants regarding the nuances of these concepts.

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.
 
Technology 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.
 
Right, the OP didn't specify a language, and yours was a good example of what he was asking for.
 
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.
 

Similar threads

  • · Replies 43 ·
2
Replies
43
Views
8K
  • · Replies 15 ·
Replies
15
Views
4K
Replies
6
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 22 ·
Replies
22
Views
2K
  • · Replies 49 ·
2
Replies
49
Views
5K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
Replies
69
Views
11K