1

not a native programmer here (medical background). I am looking for a bitwise operator that gives me the following results given the following conditions. The programming language is Python but that should not be too relevant:

condition1 condition2 result
0          1          0
1          1          1
1          0          1
0          0          1

Is this achievable with one operator alone? How would the code look like? I know it can be achieved with if/else statements but am interested in understanding new fields of programming.

8
  • 1
    Best I can think of is ~(condition1 ^ condition2) | condition1 Commented Jul 30, 2020 at 16:05
  • @0x5453: Thanks. How would that work? Commented Jul 30, 2020 at 16:05
  • 1
    Your bio says "I do have 20+ years experience in programming, mainly in C, C++, PHP and Python with a bit of MySQL and JavaScript as well". I think you can safely call yourself a programmer. Commented Jul 30, 2020 at 16:08
  • 2
    It's everything except the first case ~condition1 & condition2. So it's condition1 | ~condition2. Commented Jul 30, 2020 at 16:08
  • 1
    @Jan Certainly. Commented Jul 30, 2020 at 18:22

3 Answers 3

3

Your expected result is 1 for every case except the first.

The first case (where condition1 is 0 and condition2 is 1) is described by ~condition1 & condition2.

The opposite of that expression is

condition1 | ~condition2

so this expression gives the expected result for every case.


As requested, some detail on the difference between logical boolean operations and bitwise operations.

If we have two boolean values A and B, and we want to perform an "or" operation on them, we use the Python operator or and we get back one boolean value.

If we have two integers A and B, and we want to perform a bitwise "or" operation on them, we use the Python operator | and we get back an int whose bits are determined by the operation's action on each bit in A and B.

For instance

If A is 12 and B is 5, then they have the following bits:

A   = 1100
B   = 0101
A|B = 1101

In this case, the result A|B is 1101 binary, which is 13 decimal.

With the bitwise "not" operator the result is a little more complex to understand.

The binary representation of the number 1 is that the smallest bit is 1 and all the other bits are zero. So when you perform ~1, you don't get zero. You get a number whose every bit is 1 except for the smallest, which is zero.

 1 = 00...001
~1 = 11...110

Because ints are stored in two's complement, 11...110 is the binary representation of the number -2.

So though the boolean not operator gives you not 1==0 (i.e. not True==False), the bitwise ~ operator gives you ~1 == -2, since every individual bit gets flipped.

Sign up to request clarification or add additional context in comments.

6 Comments

Where could I find additional information for deMorgan‘s rule.
On wikipedia, for instance
Is there a way to actually represent this in Python? 0 | ~1 gives -2.
@HTF If you want to use this in Python as a boolean (as opposed to bitwise) expression it would be condition1 or not condition2.
@khelwood: Could you elaborate a bit on the difference between bitwise and bool operators - this would turn a good answer into a gret one, imo.
|
1

You could use condition1**condition2

>>> 0**1
0
>>> 1**1
1
>>> 1**0
1
>>> 0**0
1

or more generally condition1 OR (NOT condition2)

Comments

1

this is clearly not (not A and B)
usind De Morgan A or not B
In bitwise meaning A | ~B

| is or, ~ is invert (not)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.