Learning Programming #1.2 The Basics: Operators

in #coding5 years ago

In my last post you learned what variables are and how to define them. Today you will learn how to perform simple operations on them.
I've ordered the operators in 4 different categories: logical, mathematical, bit-wise and comparative.
In addition I decided to add tasks to show that you understood what I am talking about which hopefully gives me some feedback if I explain it well enough. You can write your results as a comment if you want me to correct them.
Screenshot from 2019-04-30 15-38-47.png

Logical Operators

logical operators can manipulate Boolean values(true/false). The simplest operator is not(or ! in java) which just inverts(changes true to false and false to true) the Boolean.
There are also operators that need two Booleans as input and give one Boolean as output. Those can be given in a logic table, where I use the description then the python and then the java operator. a and b are two different Booleans which can have 2 different values each which leads to 4 different values in total:

abANDORXOR
pythonoperatora and ba or ba != b
javaoperatora && ba ∣∣ ba ^ b
falsefalsefalsefalsefalse
falsetruefalsetruetrue
truefalsefalsetruetrue
truetruetruetruefalse

XOR is not used as often as the others but still might be important. More exotic output combinations can be achieved using combinations of just AND(or OR) and NOT, but those are used not very often. For example a || b(I'm using the java representation because it is shorter) can be displayed as !((!a) && (!b))

Task 1

Find a representation using only AND and NOT that only gives true when a is true and b is false.

Mathematical Operators

These operators are commonly known from maths and therefor don't need additional explanation:

mathematical Representationjava operatorpython operator
-a(negative number)-a-a
a+b(addition)a+ba+b
a-b(subtraction)a-ba-b
a×b(multiplication)a*ba*b
a÷b(division)a/ba/b
a mod b(gives the remainder of the division a÷b)a%ba%b
aᵇ(a to the power of b)not available as operatora**b

As you can see mathematical operators are usually consistent throughout multiple languages.

Task 2

This one is more about learning how to find out things you may need in programming through the internet without annoying people at StackOverflow:
How can you make aᵇ in java?

Bitwise operators

These operators are only used in special cases or in performance optimizations. As a beginner you won't use them, but it might be useful to know them.
Every number in your computer consists of bits and bits are nothing more then Boolean values(1 → true, 0 → false). So you can perform logical operations on every single bit. If you have two numbers and perform a bitwise operation on them, then for each bit in the number the bit of the result number is the result of the logical operation of bit of the first number and the bit of the second number. Bitwise operators are the same in java and python:

logical operationcorresponding bitwise operator
NOT a~a
a AND ba & b
a OR ba ∣ b
a XOR ba ^ b

Then there are also the bit-shift operators. They just simply shift the bit-sequence by a certain number of bits to the left or right. Due to the binary representation of 2(which is 10₂). This bitshift represents a multiplication or division with 2, similar to how in our decimal system a multiplication of a power of 10 just means a shift by the power(463*10² = 46300→shift of two digits to the left). So a bitshift can be understood as a faster way of multiplying with with a power of two.
There are two bitshifts(leftshift(a << b; corresponds to a*2ᵇ) and rightshift(a >> b; corresponds to a/2ᵇ)).

Task 3

This task might be hard for those who don't know binary, so I will add the binary representation of each number as a hint when necessary. n is an integer(int).
a) Find a representation of 8*n+5 using only bitwise operators. Hint: 5 = 101₂
b) Find a representation of ((n << 3) + (n << 1)) | 1 using only mathematical operators. This one can be done with only 2 mathematical operators. Hint: 10 = 1010₂
c) Find the exact result of n << 36. Hint: #1.1

Comparative Operators

These operators can compare two values and return a Boolean as a result. These could also be interpreted as a subsection of mathematical operators, but because they don't result in a number I decided to put them in an extra group.

mathematical Representationjava operatorpython operator
a = b(a equals b)a == ba == b
a ≠ b(a not equals b)a != ba != b
a ≶ b(a is smaller or bigger than b, same as a ≠ b)not available as operatora <> b
a > b(a is bigger than b)a > ba > b
a < b(a is smaller than b)a < ba < b
a ≥ b(a is bigger than or equal to b)a >= ba >= b
a ≤ b(a is smaller than or equal to b)a <= ba <= b

There are also other operators like assignment operators. The most important operator(the = operator) is used to assign a value to a variable as mentioned in #1.1.

The only difficulty in using operators is understanding the mathematical background. If you are unsure about the mathematical nature of any operator don't hesitate to ask.

Sort:  

Congratulations @quantumdeveloper! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You got more than 100 replies. Your next target is to reach 200 replies.

You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.33
TRX 0.11
JST 0.034
BTC 66579.21
ETH 3282.19
USDT 1.00
SBD 4.30