{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercice 5" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def table_verite_2bits(fonctionbool):\n", " print('|{:^10}|{:^10}|{:^15}|'.format('a','b',fonctionbool.__name__+'(a,b)'))\n", " for a in [True, False]:\n", " for b in [True, False]:\n", " print('|{:^10}|{:^10}|{:^15}|'.format(int(a), int(b), int(fonctionbool(a,b))))" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| a | b | __or__(a,b) |\n", "| 1 | 1 | 1 |\n", "| 1 | 0 | 1 |\n", "| 0 | 1 | 1 |\n", "| 0 | 0 | 0 |\n" ] } ], "source": [ "table_verite_2bits(bool.__or__)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def table_verite_2bitsV2(fonctionbool):\n", " print('|{:^10}|{:^10}|{:^15}|'.format('a','b',fonctionbool.__name__+'(a,b)'))\n", " for a in [1, 0]:\n", " for b in [1, 0]:\n", " print('|{:^10}|{:^10}|{:^15}|'.format(bool(a), bool(b), int(fonctionbool(bool(a),bool(b)))))\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| a | b | __or__(a,b) |\n", "| 1 | 1 | 1 |\n", "| 1 | 0 | 1 |\n", "| 0 | 1 | 1 |\n", "| 0 | 0 | 0 |\n" ] } ], "source": [ "table_verite_2bitsV2(bool.__or__)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| a | b | __xor__(a,b) |\n", "| 1 | 1 | 0 |\n", "| 1 | 0 | 1 |\n", "| 0 | 1 | 1 |\n", "| 0 | 0 | 0 |\n" ] } ], "source": [ "table_verite_2bitsV2(bool.__xor__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Opérations bit à bit" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Préfixe réseau" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "def ip2liste(ip):\n", " return [int(champ) for champ in ip.split('.')]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "def liste2ip(ipliste):\n", " return '.'.join(str(n) for n in ipliste)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def prefixe_reseau(ip, masque):\n", " \"\"\"Retourne le prefixe réseau à partir d'une adresse IP à partir dun masque\n", " Vérification sur http://cric.grenoble.cnrs.fr/Administrateurs/Outils/CalculMasque/\n", " \"\"\"\n", " ipliste = ip2liste(ip)\n", " masqueliste = ip2liste(masque)\n", " return liste2ip([ipliste[k] & masqueliste[k] for k in range(4)])" ] }, { "cell_type": "code", "execution_count": 128, "metadata": {}, "outputs": [], "source": [ "def suffixe_machine(ip, masque):\n", " ipliste = ip2liste(ip)\n", " masqueliste = ip2liste(masque)\n", " prefixeliste = ip2liste(prefixe_reseau(ip, masque))\n", " return liste2ip([ipliste[k] ^ prefixeliste[k] for k in range(4)])" ] }, { "cell_type": "code", "execution_count": 129, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "145.245.8.0\n", "0.0.3.254\n" ] } ], "source": [ "print(prefixe_reseau('145.245.11.254','255.255.252.0'))\n", "print(suffixe_machine('145.245.11.254','255.255.252.0'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Recherche dichotomique" ] }, { "cell_type": "code", "execution_count": 130, "metadata": {}, "outputs": [], "source": [ "def recherche_dicho2(L, e):\n", " x = 0\n", " n = len(L)\n", " pas = n >> 1\n", " while pas >= 1:\n", " while x + pas < n and L[x + pas] <= e:\n", " x = x + pas\n", " pas = pas >> 1\n", " return L[x] == e" ] }, { "cell_type": "code", "execution_count": 131, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 131, "metadata": {}, "output_type": "execute_result" } ], "source": [ "recherche_dicho2([1,4,8,10],4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Cacher un nombre dans un autre" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [], "source": [ "public = 1 << 3" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "public" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [], "source": [ "secret = 3" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [], "source": [ "public = public | secret" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "public" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b1011'" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(public)" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "public & ((1 << 2) - 1)" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0b100000000'" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bin(1 << 8)" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "127143" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ord(\"\\U0001F0A7\")" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'🂧'" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chr(127143)" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "🂭\n" ] } ], "source": [ "print(\"\\U0001F0AD\")" ] }, { "cell_type": "code", "execution_count": 111, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "🂧\n" ] } ], "source": [ "sept_pique = \"\\U0001F0A7\"\n", "print(reine_pique)" ] }, { "cell_type": "code", "execution_count": 119, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "🃍\n" ] } ], "source": [ "reine_carreau = \"\\U0001F0CD\"\n", "print(reine_carreau)" ] }, { "cell_type": "code", "execution_count": 120, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "j\n" ] } ], "source": [ "difference = ord(reine_carreau) ^ ord(reine_pique)\n", "print(chr(difference))" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "106" ] }, "execution_count": 121, "metadata": {}, "output_type": "execute_result" } ], "source": [ "difference" ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'🂧'" ] }, "execution_count": 122, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chr(ord(reine_carreau) ^ difference)" ] } ], "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.5" } }, "nbformat": 4, "nbformat_minor": 2 }