diff options
Diffstat (limited to 'semestre 3/architecture des ordinateurs')
| -rw-r--r-- | semestre 3/architecture des ordinateurs/1- Représentation de l'information.md | 18 | ||||
| -rw-r--r-- | semestre 3/architecture des ordinateurs/scripts/bin_to_hex.exs | 38 |
2 files changed, 40 insertions, 16 deletions
diff --git a/semestre 3/architecture des ordinateurs/1- Représentation de l'information.md b/semestre 3/architecture des ordinateurs/1- Représentation de l'information.md index 9545578..ee2a51c 100644 --- a/semestre 3/architecture des ordinateurs/1- Représentation de l'information.md +++ b/semestre 3/architecture des ordinateurs/1- Représentation de l'information.md @@ -240,21 +240,7 @@ def bin_to_hex(b): return hex ``` -```elixir -def bin_to_hex(b) do - bin_to_hex_rec(b, []) -end - -defp bin_to_hex_rec(before, after) when before != "" do - {b, a} = String.split_at(before, -4) - #TODO: convert algo - bin_to_hex_rec(b, [a | after]) -end - -defp bin_to_hex_rec("", after) do - after -end -``` +Voir le dossier `scripts` pour la version elixir. Pour réaliser une addition en binaire, on a besoin que les deux mots fassent la même taille @@ -290,4 +276,4 @@ $$ = \mathrm{0b}1\ldots1 = -1 $$ Donc, on a que $\bar N$ doit être décalé de 1, d'où le plus 1 |> cela est dû au fait que l'intervalle ne soit pas symétrique -Voir le moodle pour la suite
\ No newline at end of file +Voir le moodle pour la suite diff --git a/semestre 3/architecture des ordinateurs/scripts/bin_to_hex.exs b/semestre 3/architecture des ordinateurs/scripts/bin_to_hex.exs new file mode 100644 index 0000000..8d6e629 --- /dev/null +++ b/semestre 3/architecture des ordinateurs/scripts/bin_to_hex.exs @@ -0,0 +1,38 @@ +defmodule Conv do + def bin_to_hex(b) do + bin_to_hex_rec(b, []) + end + + defp bin_to_hex_rec(before, aft) when before != "" do + {b, a} = String.split_at(before, -4) + s = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"] + bin_to_hex_rec(b, [Enum.at(s, bin_to_dec(a)) | aft]) + end + + defp bin_to_hex_rec("", aft) do + aft + end + + def bin_to_dec(b) do + bin_to_dec_rec(b, 0, 0) + end + + defp bin_to_dec_rec(before, aft, i) when before != "" do + {b, a} = String.split_at(before, -1) + bin_to_dec_rec(b, case a do + "0" -> aft + "1" -> aft + Integer.pow(2, i) + end, i + 1) + end + + defp bin_to_dec_rec("", aft, _i) do + aft + end +end + +Conv.bin_to_hex("1111") +|> IO.puts() # must print f + +Conv.bin_to_hex("11101011") +|> IO.puts() # must print EB + |
