Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualRadikalizm

Posted 18 November 2012 - 08:32 AM

IMO the simplest way to explain the basic logical operators is by using truth tables. Let's have a look at the truth table for a logical AND with all possible input values for 2 random propositions (P and Q).

The leftmost column shows all values for P, the middle column all values for Q, and the rightmost column the results of the AND operation on the two inputs.








P AND (&) Q
PQP AND Q
000
010
100
111


Now, how do we translate this operation to actual integer values which can range far beyond the values contained in a single bit (as portrayed in this truth table)?. It's quite easy actually, let's take a look at some 8-bit (1 byte) values:

We take 2 random values, let's say 0xF8 (248 decimal) and 0x15 (21 decimal). Let's lay these values out in binary form:

0xF8: 1 1 1 1 1 0 0 0
0x15: 0 0 0 1 0 1 0 1

When we look at these numbers in a binary form it gets much easier to do a logical AND operation on them. We iterate over the presented binary numbers in the first integer and we do a logical AND with the matching binary value in the other integer.
Let's have a look at the result:

1 1 1 1 1 0 0 0
0 0 0 1 0 1 0 1 (AND)
-------------------
0 0 0 1 0 0 0 0

If we put the resulting binary number back into a hexadecimal form we get 0x10 (16 decimal).

So when you use the &-operator in C++ on the values 0xF8 and 0x15 you would get 0x10 as a result (0xF8 & 0x15 == 0x10)

EDIT:

Wow, it seems like I take a very long time to post :D

#1Radikalizm

Posted 18 November 2012 - 08:30 AM

IMO the simplest way to explain the basic logical operators is by using truth tables. Let's have a look at the truth table for a logical AND with all possible input values for 2 random propositions (P and Q).

The leftmost column shows all values for P, the middle column all values for Q, and the rightmost column the results of the AND operation on the two inputs.








P AND (&) Q
PQP AND Q
000
010
100
111


Now, how do we translate this operation to actual integer values which can range far beyond the values contained in a single bit (as portrayed in this truth table)?. It's quite easy actually, let's take a look at some 8-bit (1 byte) values:

We take 2 random values, let's say 0xF8 (248 decimal) and 0x15 (21 decimal). Let's lay these values out in binary form:

0xF8: 1 1 1 1 1 0 0 0
0x15: 0 0 0 1 0 1 0 1

When we look at these numbers in a binary form it gets much easier to do a logical AND operation on them. We iterate over the presented binary numbers in the first integer and we do a logical AND with the matching binary value in the other integer.
Let's have a look at the result:

1 1 1 1 1 0 0 0
0 0 0 1 0 1 0 1 (AND)
-------------------
0 0 0 1 0 0 0 0

If we put the resulting binary number back into a hexadecimal form we get 0x10 (16 decimal).

So when you use the &-operator in C++ on the values 0xF8 and 0x15 you would get 0x10 as a result (0xF8 & 0x15 == 0x10)

PARTNERS