Binary Operators¶

you can watch parallel the video for C/C++ (german): https://www.youtube.com/watch?v=HDB-r4PEVP4&list=PLEWVM-KBUSpmWSfyoFdD_hLWAY_9tTgi5&index=14

In [1]:
import ROOT
In [2]:
import fortranmagic
%load_ext fortranmagic
import os
import sys

import numpy as np

if sys.platform.startswith("win"):
        # Depends of system, python builds, and compilers compatibility.
        # See below.
    f_config = "--fcompiler=gnu95 --compiler=mingw32"
else:
        # For Unix, compilers are usually more compatible.
    f_config = ""

    # Disable only deprecated NumPy API warning without disable any APIs.
f_config += " --extra '-DNPY_NO_DEPRECATED_API=0'"

%fortran_config {f_config}
New default arguments for %fortran:
	 --extra '-DNPY_NO_DEPRECATED_API=0'

Bitwise operators¶

C/C++¶

~ is the bitwise unary complement operator. For unsigned numbers it gives the maximal value that can be displayed. If you use it on signed numbers like int, then you get the binary complement but remember that the most significant bit is used as a sign and 11111111111111111111111111111111 would represent -1, and 11111111111111111111111111111110 would represent -2. This is done that addition works as normal, if you add 1 to -2 you get -1.

In [3]:
%%cpp
unsigned int u=0;
int i=0;
int j=1;

cout <<~u<<endl;
cout <<~i<<endl;
cout <<~j<<endl;
4294967295
-1
-2

The << and >> are binary left and right shift operators. Remark: In c++ you can redefine operators. cout << is and example it is a redifined shift operator, so when we want to use << on numbers we need to put a bracket around it when used inside cout expressions:

In [4]:
%%cpp
cout <<(1<<30)<<endl;
//here we shift the 1 into the most significant bit
cout <<(1<<31)<<endl;//integer overflow
cout <<((unsigned int)(1)<<31)<<endl;
cout <<(1<<8)<<endl;
cout <<(256>>8)<<endl;
1073741824
-2147483648
2147483648
256
1

The bitwise binary operators & (and) | (or) and ^ (xor) have. Attention due to historic reasons - they were used in c in early times as logical operators (see below) they have lower precedence as the comparison operators (in c/c++ and javascript). This is different in python..

In [5]:
%%cpp
cout <<(6 & 3)<<endl;
cout <<(6&3==2)<<endl;//produces a warning
cout <<((6&3)==2)<<endl;
2
0
1
input_line_58:3:10: warning: & has lower precedence than ==; == will be evaluated first [-Wparentheses]
cout <<(6&3==2)<<endl;//produces a warning
         ^~~~~
input_line_58:3:10: note: place parentheses around the '==' expression to silence this warning
cout <<(6&3==2)<<endl;//produces a warning
         ^    
          (   )
input_line_58:3:10: note: place parentheses around the & expression to evaluate it first
cout <<(6&3==2)<<endl;//produces a warning
         ^ 
        (  )

Javascript¶

In [6]:
%%js //the next line is only necessary in jupyter notebooks
element.setAttribute('style', 'white-space: pre;');console.log=function(text){element.textContent+=text+"\n"}

console.log(6&3)
console.log(6&3==2)
console.log((6&3)==2)
In [7]:
%%js //the next line is only necessary in jupyter notebooks
element.setAttribute('style', 'white-space: pre;');console.log=function(text){element.textContent+=text+"\n"}

console.log(1<<31)//integer overflow
console.log(1<<31>>31)//so here we get not one
console.log(1<<31>>>31)//that's why javascript has a unsigned right shift operator

Python¶

Python does not have unsigned numbers.

In [8]:
print(~1)
-2

But it is not so problematic as python does not have integer overflow.

In [9]:
print(1<<12800)
15273708540796677855567598230857492232616230932137438752083864194096252035158298139783034803039562299865591895860374533691805527863383163337167716810184021237322415432768428800537248505174462397583693728185764778775698717134048189987560569377651799042341033408012137054904634866948467757538875101112443345310542511917704877822716053748531806030068618457501537687744155974418763909049249484556849027368572076327167033939378152697530994164332042607094399070976592988811140984716624318429813271819813801492481467571355863520789869737007105344107797892610611132322496403212162457092878082920116137773839224178060324642891339319714674583179898760155164009158559488521247797953447590966762668514458007321679435494403106937304281596355142283741361865544861092988938771573688300178705265180686588755396772425825447097936377920458059366407959451344680083846791907539018956606630471730098629110976441095853009099461143713576028941669526048906511184844170621232398898492574810551983834117973354738161761343453645258450559749626458062310729724900800667056817819866503140715738677620309486546788912112998836295020453462407332871651651530328524215592500405647004309884635973215663744461415638873565792854251505561822881159357157327938810719692553851994562227176654546538376569126626284985940080744654994397383022653543163694226007708457081577836672617208015546573713983946619812259401367488146096049028876695872070762727752379249245429585379754165623555795711122812668354570207429293883127272266534798723252667844000260216909006070809661492656454851287200740688117226124291240154416703109381133580320445598336585145434828406891526534968343848044977977734538795357821534672259364105105264046421079893645584277634191303420718467779663965363547693592720001324009104938651461940901074651420218490264939030753717409073901898255647018892810703015355878474739651407124179192374845484996419029432352945827162155207686322590185997972752292121490151025350630861288960973703761621730645921850275024926452378331997053906694392672214477359919911931508189689829738920022414322699515390014973553062304241994935636521211320449484698257270340551640421858633864974146546260465070707783180737356344025474670097297135354529636470247355633283447734563318902067110993098395868200537961656815027154996962501216721218485883511700415721708293608759031236546798651146955034834080985583979692340787598927123316324084041972677036906633803170128604647540961008302302892348142903797634354131483846686794523985205316852871865363362564450242026324306058007861855322012521391870499799327921748518746001205794076658075380370867382396187987817834680400801213921412651739260917694838207095188562904912106158826432148403304687696006445457516544286633959303055263748009914318049479356180768956803729447839373235798593001299138377149162471505275682001007291409907409850340831095456985200352604633386936999538732829507159401819922862792068730648073238014719410086901066759148630919712710814757798767179667301116856314317498137845053774537063794275661452176527636328980753241673486262127905740676727527108414391015207367895144042817626668255171423944169632570102180211268757267960679886481175083695231704515988970220352116019177919108108941547789684583805849159907686274163174043846459339508753310604633028326926327683020939448151544347626759957789995071867037504384909056826221791231697521517420780099106259080048972732554727455640127702447400049502893808211093520243224087210662564668293302155869602721826639227190866352301173662821819699636445235976297255999046040196947690092126674560045062306643386292766514030774952727519175606038174437042335727803810607263782792238862211922517995450242406075681288821452862024353516989000668312440386255445832488166836828399387599741469798947716557717616221159335568153797724365799092918963885827534917764782735837729951118559816187113143943248580581480800450695397376
In [10]:
print(6&3)
print(6&3==2)
2
True

Fortran¶

in fortran binary operators are implemented as function

In [11]:
%%fortran 

! program and subroutine exchanged due to jupyternotebook

! program main
subroutine main()
    implicit none
    integer :: a = 6,b=3
    print *,iand(a,b)
    print *,ior(a,b)
    print *,ieor(a,b)
    ! print *,inot(a)
    print *,ishft(a,3)  ! leftshift
    print *,ishft(a,-3) ! right shift
    print *,ibset(8,1) ! set bit 1
    print *,ibclr(7,1) ! clear bit 1
! end program
end subroutine main
In [12]:
main()
           2
           7
           5
          48
           0
          10
           5

Comparison operators¶

C++ and C¶

The precedence rules for C++/C and Javascript are the same. But they are different for Python and as mentioned earlier are evaluated also in a special way.

In [13]:
%%cpp
cout <<(1<2)<<(1<=2)<<(1>2)<<(1>=2)<<(1==2)<<(1!=2);
110001

Because == and != have higher precedence than < <= > >= in c++/c and javascript. This is valid.

In [14]:
%%cpp
cout <<(1<2==true); 
cout <<(3>2>1);
cout <<((3>2)>1);
100
In [15]:
print((1<2)==True)
print(1<2 and 1==True)
True
True

Javascript¶

In [16]:
%%js //the next line is only necessary in jupyter notebooks
element.setAttribute('style', 'white-space: pre;');console.log=function(text){element.textContent+=text+"\n"}

console.log(1<2==true)
console.log(3>2>1)
console.log((3>2)>1)

Python¶

In [17]:
print(1<2==True)
print(1 and 2==True)
print(3>2>1)
print((3>2)>1)
print(3>2 and 2>1)
False
False
True
False
True

Logic operators¶

c++¶

The logic operators combine booleans so true and false. They are && (and), || (or) and ! (not). Attention a common mistake is to write & and | instead of && and ||. So thats why in c++ you can use and, or and not. A very important concept with logic operators is short circuit evaulation. Meaning that the rest of the logic operation is skipped if the result is clear. This has important impact when as a part of the logic operation a function is called.

In [18]:
%%cpp
cout <<!(true && true || false)<<endl;
cout <<not(true and true or false)<<endl;
0
0

In all 4 langanges the logic operators have lower precedence than the comparison operators and consequently the following expressions do no need brackets.

In [19]:
%%cpp
cout <<(1<2 && 3<4 || 4<3);
1
In [20]:
%%cpp

2<1 or cout<<"wrong.";
2<1 || printf("wrong");//also works in c
wrong.wrong

C¶

It is the same as in C++ just that and, or and not are not allowed. And that the result is not of type bool but of int.

javascript¶

javascript follows c (not c++).

In [21]:
%%js //the next line is only necessary in jupyter notebooks
element.setAttribute('style', 'white-space: pre;');console.log=function(text){element.textContent+=text+"\n"}

console.log(!(1<2 && 3<4 || 4<3))
In [22]:
%%js //the next line is only necessary in jupyter notebooks
element.setAttribute('style', 'white-space: pre;');console.log=function(text){element.textContent+=text+"\n"}

2<1 || console.log("that is wrong")

Python¶

Python only has the written out notation and, or and not.

In [23]:
print(not (1<2 and 3<4 or 4<3))
False
In [24]:
2<1 or print("wrong")
wrong

Fortran¶

program main
implicit none

print *,.not.(1 .le. 2 .and. 3 .le. 4 .or. 4 .le. 3)
print *,.not.(1 < 2 .and. 3 < 4 .or. 4 < 3)

read *
end program
In [25]:
%%fortran 

! program and subroutine exchanged due to jupyternotebook

! program main
subroutine main()
    implicit none

    print *,.not.(1 .le. 2 .and. 3 .le. 4 .or. 4 .le. 3)
    print *,.not.(1 < 2 .and. 3 < 4 .or. 4 < 3)
! end program
end subroutine main
In [26]:
main()
 F
 F