{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Practical example of Bitwise operators in Python\n",
    "\n",
    "Trainer: Md. Jalal Uddin\n",
    "    \n",
    "Founder and director of Research Society https://researchsociety20.org/founder-and-director/\n",
    "\n",
    "email: 20205103002@nuist.edu.cn, dmjalal90@gmail.com, "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# What is Bitwise operator in Python?\n",
    "\n",
    "convert integers into binary\n",
    "\n",
    "bit by bit operation\n",
    "\n",
    "output as decimal format\n",
    "\n",
    "decimal [base 10 (0-9)]\n",
    "\n",
    "binary [base 2 (0-1)]\n",
    "\n",
    "binary = prefix 0b\n",
    "\n",
    "\n",
    "Python's \"and\", \"or\" and \"not\" logical operators are designed to work with \"scalars\".\n",
    "\n",
    "Read more: https://stackoverflow.com/questions/21415661/logical-operators-for-boolean-indexing-in-pandas\n",
    "\n",
    "However, \"&\", \"|\" and \"~\" Bitwise operators are designed to work with both \"scalars\" and \"vectors\".\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Learn more from geeksforgeeks\n",
    "\n",
    "https://www.geeksforgeeks.org/python-bitwise-operators/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# &\n",
    "\n",
    "If both the bits are 1, it returns 1; otherwise, it returns 0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# |\n",
    "\n",
    "If either of the bit is 1, it returns 1; otherwise, it returns 0\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ~\n",
    "\n",
    "It returns one’s complement of the number\n",
    "\n",
    "For example:\n",
    "\n",
    "NOT x = -x − 1     \n",
    "\n",
    "https://www.pyblog.in/programming/bitwise-operators-in-python/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# define variable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 4\n",
    "b = 12"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "decimal to binary for a = 0b100\n",
      "decimal to binary for b = 0b1100\n"
     ]
    }
   ],
   "source": [
    "print(\"decimal to binary for a =\",bin(a))\n",
    "\n",
    "print(\"decimal to binary for b =\",bin(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a & b = 4\n",
      "a | b = 12\n",
      "~a = -5\n"
     ]
    }
   ],
   "source": [
    "print(\"a & b =\", a&b)    \n",
    "                           \n",
    "print(\"a | b =\", a | b)  \n",
    "    \n",
    "print(\"~a =\", ~a)          "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a & b = 4\n",
      "a | b = 12\n"
     ]
    }
   ],
   "source": [
    "import operator as op\n",
    "\n",
    "print(\"a & b =\", op.__and__(a, b))\n",
    "\n",
    "print(\"a | b =\", op.__or__(a, b))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Example for real data\n",
    "\n",
    "We will monitor drought (water scarcity) for Rangpur station in Bangladesh from 1994 to 1995\n",
    "\n",
    "The example has been adapted from Uddin et al. (2020). https://link.springer.com/article/10.1007/s12517-020-05302-0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Year and month</th>\n",
       "      <th>SPI</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1983-01</td>\n",
       "      <td>-0.285149</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1983-02</td>\n",
       "      <td>-0.299666</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1983-03</td>\n",
       "      <td>-0.229297</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1983-04</td>\n",
       "      <td>-0.382996</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1983-05</td>\n",
       "      <td>-0.060437</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Year and month       SPI\n",
       "0        1983-01 -0.285149\n",
       "1        1983-02 -0.299666\n",
       "2        1983-03 -0.229297\n",
       "3        1983-04 -0.382996\n",
       "4        1983-05 -0.060437"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "dataset = pd.read_excel('drought_data.xlsx')\n",
    "\n",
    "dataset.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "133    0.289110\n",
       "134    0.291538\n",
       "135    0.238511\n",
       "136    0.236418\n",
       "137   -0.093880\n",
       "Name: SPI, dtype: float64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "SPI = dataset.SPI[133:156]   # 1994-1995\n",
    "\n",
    "SPI.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "The climate was wet if the SPI values are greater than 1 and less than 2. Similarly, if the SPI values are less than 1 and less than equal -2, the climate was dry.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wet climate = \n",
      " 133    False\n",
      "134    False\n",
      "135    False\n",
      "136    False\n",
      "137    False\n",
      "138    False\n",
      "139    False\n",
      "140    False\n",
      "141    False\n",
      "142    False\n",
      "143    False\n",
      "144    False\n",
      "145    False\n",
      "146    False\n",
      "147    False\n",
      "148    False\n",
      "149    False\n",
      "150    False\n",
      "151    False\n",
      "152    False\n",
      "153    False\n",
      "154    False\n",
      "155    False\n",
      "Name: SPI, dtype: bool\n"
     ]
    }
   ],
   "source": [
    "print(\"wet climate = \\n\",SPI.gt(1) & SPI.lt(2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dry climate = \n",
      " 133    True\n",
      "134    True\n",
      "135    True\n",
      "136    True\n",
      "137    True\n",
      "138    True\n",
      "139    True\n",
      "140    True\n",
      "141    True\n",
      "142    True\n",
      "143    True\n",
      "144    True\n",
      "145    True\n",
      "146    True\n",
      "147    True\n",
      "148    True\n",
      "149    True\n",
      "150    True\n",
      "151    True\n",
      "152    True\n",
      "153    True\n",
      "154    True\n",
      "155    True\n",
      "Name: SPI, dtype: bool\n"
     ]
    }
   ],
   "source": [
    "print(\"dry climate = \\n\",SPI.lt(1) | SPI.le(-2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "not extreme wet climate = \n",
      " 133    True\n",
      "134    True\n",
      "135    True\n",
      "136    True\n",
      "137    True\n",
      "138    True\n",
      "139    True\n",
      "140    True\n",
      "141    True\n",
      "142    True\n",
      "143    True\n",
      "144    True\n",
      "145    True\n",
      "146    True\n",
      "147    True\n",
      "148    True\n",
      "149    True\n",
      "150    True\n",
      "151    True\n",
      "152    True\n",
      "153    True\n",
      "154    True\n",
      "155    True\n",
      "Name: SPI, dtype: bool\n"
     ]
    }
   ],
   "source": [
    "print(\"not extreme wet climate = \\n\", ~(SPI>2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wet climate = \n",
      " 133    False\n",
      "134    False\n",
      "135    False\n",
      "136    False\n",
      "137    False\n",
      "138    False\n",
      "139    False\n",
      "140    False\n",
      "141    False\n",
      "142    False\n",
      "143    False\n",
      "144    False\n",
      "145    False\n",
      "146    False\n",
      "147    False\n",
      "148    False\n",
      "149    False\n",
      "150    False\n",
      "151    False\n",
      "152    False\n",
      "153    False\n",
      "154    False\n",
      "155    False\n",
      "Name: SPI, dtype: bool\n"
     ]
    }
   ],
   "source": [
    "print(\"wet climate = \\n\",op.__and__(SPI.gt(1), SPI.lt(2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dry climate = \n",
      " 133    True\n",
      "134    True\n",
      "135    True\n",
      "136    True\n",
      "137    True\n",
      "138    True\n",
      "139    True\n",
      "140    True\n",
      "141    True\n",
      "142    True\n",
      "143    True\n",
      "144    True\n",
      "145    True\n",
      "146    True\n",
      "147    True\n",
      "148    True\n",
      "149    True\n",
      "150    True\n",
      "151    True\n",
      "152    True\n",
      "153    True\n",
      "154    True\n",
      "155    True\n",
      "Name: SPI, dtype: bool\n"
     ]
    }
   ],
   "source": [
    "print(\"dry climate = \\n\",op.__or__(SPI.lt(1), SPI.le(-2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
