#can smb explain what is the difference between bitwise and logical operators?.

1 messages · Page 1 of 1 (latest)

hardy brook
#

i dont understand at all. and the more i google the less i get it.

twin dirgeBOT
#

<@&987246964494204979> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
I dont understand at all. and the more i google the less i get it.

Bitwise operators and logical operators are both used in programming to manipulate binary values, but they serve different purposes.

Bitwise operators (such as &, |, ^, ~, <<, >>, >>>) operate on individual bits of binary numbers. They perform operations like AND, OR, XOR, bitwise negation, left shift, and right shift. Bitwise operators are used to manipulate bits within a number for tasks like setting or clearing specific bits or extracting certain information from a binary value.

On the other hand, logical operators (such as &&, ||, !) operate on boolean values (true or false). They are used to evaluate conditions and make decisions in control structures like if statements and loops. Logical operators perform operations like AND, OR, and NOT on boolean values to determine the truthfulness of an expression.

In summary:

  • Bitwise operators work at the bit level and manipulate individual bits.
  • Logical operators work with boolean values and determine the truth of a statement based on conditions.

It's important to understand when to use bitwise and logical operators based on the specific requirements of your program. Bitwise operators are commonly used in tasks that involve low-level manipulation of data at the bit level, while logical operators are more often used in higher-level decision-making processes.

tame night
#

as the name implies, bitwise operators are first and foremost used to do bit operations

#

for example on two ints

#

20 & 53 is for example 20

#

20 ^ 53 is 33

#

and so on

#

but i suspect ur question wasnt that

#

but instead what happens when u use them on booleans

#

such as true & false

#

vs true && false

#

in which case i first have to explain that true & false is NOT using a bitwise operator

#

this is just incorrect terminology

#

& (used on boolean) and && are both logical operators

#

(same goes for | and ||)

#

they also behave mostly identical

#

like, the truth table is the same for both

#

the difference is that && and || are whats known as short-circuiting. or "smart"

#

they dont fully evaluate/execute the expression if the result is known already

#

example:

#

foo() && bar()

#

lets say foo() returns false

#

in this case, its clear that the result can only be false, since "false and something else" can only be false

#

&& is smart and aborts now

#

it does NOT call bar()

#

foo() & bar() will fully execute it, not aborting. so it DOES call bar()

#

ofc the result is in both cases the same, false

#

but the difference is in whether bar() is executed or not

#

similar for | vs ||

#

true | bar() will execute bar()

#

true || bar() wont

hardy brook
#

select * from customercontacts where type &1!=1 --mobile and type &4!=4;

what does it even mean? how do i know is it logical operator of binary?

covert crest
#

That's an sql question

#

But if you have numbers, it's a bitwise operator, if you have booleans, it's a logical operator

#
select * from customercontacts where type &1!=1 --mobile and type &4!=4;```
#

As you can see from the highlight, this query has half of it commented

twin dirgeBOT
#

@hardy brook

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure 👍

covert crest
#

No that's a third class, called comparison operator

#

See in this page, there's different kinds of operators