reflexion sur les conversions binaires

This commit is contained in:
Hugo LAMY
2022-02-09 22:48:44 +01:00
parent a653ad05a9
commit 57663f5285
4 changed files with 270 additions and 0 deletions

197
d02/ex00/Fixed.hpp Normal file
View File

@@ -0,0 +1,197 @@
#ifndef FIXED_HPP
# define FIXED_HPP
#include <iostream>
#include <string>
class NameFile {
public:
Fixed( void ); // default/parametric constructor
Fixed( Sample const & src ); // copy constructor
~Fixed( void ); // destructor
Fixed & operator=( Namefile const & rhs ); // assignement operator
private:
};
#endif
//
//
// 11010101
//
// 1*10000000 | (128) | 2^7 | 128 *1 | 0 *2 +1
// 1*1000000 | (64) | 2^6 | + 64 *1 | 1 *2 +1
// 0*100000 | (32) | 2^5 | | 11 *2
// 1*10000 | (16) | 2^4 | + 16 *1 | 110 *2 +1
// 0*1000 | (8) | 2^3 | | 1101 *2
// 1*100 | (4) | 2^2 | + 4 *1 | 11010 *2 +1
// 0*10 | (2) | 2^1 | | 110101 *2
// 1*1 | (1) | 2^0 | + 1 *1 | 1101010 *2 +1
// | ______ |
// | 213 |
// 213
//
// 2*100 |(1100100)| 10^2 | 1100100 *2 | 0 *10 +2
// 1*10 | (1010)| 10^1 | + 1010 *1 | 2 *10 +1
// 3*1 | (1)| 10^0 | + 1 *3 | 21 *10 +3
// | | | ___________ | 213
// | | | 11010101 |
// |
// |
// 1100100 11001000 1 11010010 |
// * 2 + 1010 * 3 + 11 |
// _________ _________ ___ _________ |
// 11001000 11010010 11 11010101 |
//
//
// 213 -> 11010101
//
// 213 -128 = 85 | 213 /2 = 106 r 1
// 85 - 64 = 2 | 106 /2 = 53 r 0
// | 53 /2 = 26 r 1
// 21 - 16 = 5 | 26 /2 = 13 r 0
// | 13 /2 = 6 r 1
// 5 - 4 = 1 | 6 /2 = 3 r 0
// | 3 /2 = 1 r 1
// 1 - 1 = 0 | 1 /2 = 0 r 1
//
// 11010101 -> 213
//
// | 0*2 + 1 = 1
// | 1*2 + 1 = 3
// | 3*2 = 6
// | 6*2 + 1 = 13
// | 13*2 = 26
// | 26*2 + 1 = 53
// | 53*2 = 106
// | 106*2 + 1 = 213
//
//
//
// 1.....................23
// 8......1
// seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm meaning
// 31 0 bit #
//
// 2x10^-1 = 0.2x10^0 = 0.02x10^1 = 0.2
//
// 5.75
// 5 :
// 5 / 2 = 2 -> 1
// 2 / 2 = 1 -> 0
// 1 / 2 = 0 -> 1
// 75 :
// .75 *2^2 = 2,88 ~= 3
// 3 / 2 = 1 -> 1
// 1 / 2 = 0 -> 1
// 101.11 = 2^2 + 2^0 + 2^-1 + 2^-2
// = 4 + 1 + 0.5 + 0.25
//
// 0.875
// .875 * 2^3 = 7 -> 0.111 * 2^3 = 111.0
// 7 / 2 = 3 -> 1
// 3 / 2 = 1 -> 1
// 1 / 1 = 0 -> 1
// 0.111
//
// -1 1875
// 0.1875 = 1.875 * 10^-1 = 01 11101010011
// 0.1875 = 0011
// 0*2^-1 + 082^-2 + 1*2^-3 + 1*2^-4
// 0 + 0 + .125 + .0625 = .1875
//
//
// -43.625
// -101011.101
// fixed :
// 1 101011 101
// -
// 101011
// 2^5 + 2^3 + 2^1 + 1
// 32 + 8 + 2 + 1 = 43
// 101
// 2^-1 + 2^-3
// .5 + .125 = .625
//
//
// -53.5 --> -110101.1
// [ fixed : ]
// 1 110101 1
// -
// 110101
// 2^5 + 2^4 + 2^2 + 1
// 32 + 16 + 4 + 1 = 53
// 1
// 2^-1
// .5 = .5
// [ float : ]
// -1.101011 * 2^5
// 11.01011 -> 1
// 110.1011 -> 2
// 1101.011 -> 3
// 11010.11 -> 4
// 110101.1 -> 5
// 1 101 101011
// -
// 2^2 + 1
// 4 + 1 = 5
// 101011
// ->1101011
//
//
//
//
// .85
// .85 * 2 = 1.7 [1] [.7]
// .7 * 2 = 1.4 [1] [.4]
// .4 * 2 = 0.8 [0] [.8]
// .8 * 2 = 1.6 [1] [.6]
// .6 * 2 = 1.2 [1] [.2]
// .2 * 2 = 0.4 [0] [.4]
// .4 * 2 = 0.8 [0] [.8]
// .8 * 2 = 1.6 [1] [.6]
// .6 * 2 = 1.2 [1] [.2]
// .2 * 2 = 0.4 [0] [.4]
// .4 * 2 = 0.8 [0] [.8]
// .8 * 2 = 1.6 [1] [.6]
// .6 * 2 = 1.2 [1] [.2]
// .2 * 2 = 0.4 [0] [.4]
// .4 * 2 = 0.8 [0] [.8]
// .8 * 2 = 1.6 [1] [.6]
// .6 * 2 = 1.2 [1] [.2]
// .2 * 2 = 0.4 [0] [.4]
// .4 * 2 = 0.8 [0] [.8]
// .8 * 2 = 1.6 [1] [.6]
// .6 * 2 = 1.2 [1] [.2]
// .2 * 2 = 0.4 [0] [.4]
// .4 * 2 = 0.8 [0] [.8]
// ...
// -> 11011001100110011001100...
// 1 1 0 1 1 0 0 1 1 0 0 1 1
// 2^-1 + 2^-2 + 0 + 2^-4 + 2^-5 + 0 + 0 + 2^-8 + 2^-9 + 0 + 0 + 2^-12 + 2^-13
// .5 + .25 + .0625 + .03125 + .00390625 + .001953125 + .000244140625 + .0001220703125
// = .8499755859375
// .5
// .75
// .8125
// .84375
// .84765625
// .849609375
// .849853515625
// .8499755859375
//
//
// .453125 *2 = 0.90625 [o] [.90625]
// .90625 *2 = 1.8125 [1] [.8125]
// .8125 *2 = 1.625 [1] [.625]
// .625 *2 = 1.25 [1] [.25]
// .25 *2 = 0.5 [0] [.5]
// .5 *2 = 1 [1] []
// -> .011101
//
//

64
d02/ex00/Makefile Normal file
View File

@@ -0,0 +1,64 @@
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# . name = value . name is case sensitive #
# VARIABLES . or name = value \ . use VPATH only for .c #
# . value . or .cpp #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
NAME = zombie
CC = clang++
CFLAGS = -Wall -Wextra -Werror $(INCLUDES) -std=c++98
VPATH = $(D_SRCS)
LIBS =
INCLUDES = -I$(D_HEADERS)
D_SRCS = .
SRCS = main.cpp \
Zombie.cpp \
newZombie.cpp \
randomChump.cpp
D_HEADERS = .
HEADERS = Zombie.hpp
D_OBJS = builds
OBJS = $(SRCS:%.cpp=$(D_OBJS)/%.o)
RM_D_OBJS = rm -rf $(D_OBJS)
ifeq "$(D_OBJS)" "."
RM_D_OBJS =
endif
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# . target: prerequisites . $@ : target #
# RULES . recipe . $< : 1st prerequisite #
# . recipe . $^ : all prerequisites #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
all: $(NAME)
$(D_OBJS)/%.o: %.cpp | $(D_OBJS)
$(CC) $(CFLAGS) -c $< -o $@
$(D_OBJS):
mkdir $@
$(OBJS): $(HEADERS:%=$(D_HEADERS)/%)
$(NAME): $(OBJS)
$(CC) $(OBJS) -o $@ $(LIBS)
clean:
rm -f $(OBJS)
fclean: clean
rm -f $(NAME)
$(RM_D_OBJS)
re: fclean all
.PHONY : all clean fclean re bonus run valgrind

9
d02/ex00/main.cpp Normal file
View File

@@ -0,0 +1,9 @@
#include "Fixed.hpp"
int main() {
return 0;
}

BIN
d02/fr.subject.pdf Normal file

Binary file not shown.