Solving Programming Doubts: Logic, Shifts & Situations

  • Thread starter Thread starter rama1001
  • Start date Start date
  • Tags Tags
    Doubt Programming
AI Thread Summary
Understanding when to use logical operators like AND and OR is crucial in programming, particularly in conditional statements. AND is used when all conditions must be true for the statement to execute, while OR is used when at least one condition must be true. For example, in Python, you can check if a string equals either "alpha" or "bravo" using OR, and if it does not equal both using AND. However, care must be taken with languages like C or C++, where string comparisons require different handling due to how strings are evaluated as addresses.Shifts, both arithmetic and logical, serve different purposes in programming. Right shifts effectively divide a number by two, while left shifts multiply it by two. It's important to note that right shifts perform integer division, discarding any fractional part. Shifts are also useful for bit manipulation, allowing for efficient data representation and extraction, particularly in scenarios where memory efficiency is critical. Using shifts in conjunction with logical operators can enhance performance in data processing tasks.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top