Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision |
other:python:misc_by_jyp [2023/05/04 11:46] – [Data representation] Added the Base notions section jypeter | other:python:misc_by_jyp [2023/05/04 14:33] – [Base notions] Improved jypeter |
---|
==== Base notions ==== | ==== Base notions ==== |
| |
* **Never forget** that all the bits and pieces of information we use are coded in [[https://en.wikipedia.org/wiki/Binary_number#Counting_in_binary|base 2]] (''0''s and ''1''s), grouped in bytes! | * **Never forget** that all the bits and pieces of information we use are coded in [[https://en.wikipedia.org/wiki/Binary_number#Counting_in_binary|base 2]] (''0''s and ''1''s ...), grouped in bytes! |
* Some things can be stored exactly (integers, characters, ...) | * Some things can be stored exactly (integers, characters, ...) |
* In other cases (**//real// numbers** that we work with all the time, compressed images/videos/music) we only store **//good enough approximation//** | * In other cases (**//real// numbers** that we work with all the time, compressed images/videos/music) we only store **//good enough approximation//** |
* 1 byte <=> 8 bits | * 1 byte <=> 8 bits |
* ''REAL*4'' <=> 4 bytes <=> 32 bits | * ''REAL*4'' <=> 4 bytes <=> 32 bits |
* For easier written/displayed representation, 1 byte is usually split into 2 groups of 4 bits, using base 16 and [[https://en.wikipedia.org/wiki/Hexadecimal|hexadecimal representation]] | * For easier written/displayed representation, 1 byte is usually split into 2 groups of 4 bits, and displayed using base 16 and [[https://en.wikipedia.org/wiki/Hexadecimal|hexadecimal representation]] (characters ''0'', ''1'', ..., ''A'', ''B'', ..., ''F'') |
* ''0000'' <=> ''0'', ''0010'' <=> ''1'', ..., ''1111'' <=> ''F'' | * ''0000'' <=> ''0'',\\ ''0010'' <=> ''1'', ...,\\ ''1111'' <=> ''F'' |
* ''1101'' <=> ''D'' in hexadecimal <=> ''13'' in decimal (''**1** * 8 + **1** * 4 + **0** * 2 + **1** * 1'') | * ''1101'' <=> ''D'' in hexadecimal <=> ''13'' in decimal (''**1** * 8 + **1** * 4 + **0** * 2 + **1** * 1'') |
* ''11111101'' <=> ''1111 1101'' <=> ''FC'' in hexadecimal <=> ''253'' in decimal (''15 * 16 + 13'') | * ''11111101'' in //base 2// <=> ''1111 1101'' <=> ''FD'' in //hexadecimal// <=> ''253'' (''15 * 16 + 13'') in //decimal// |
| |
* Conversion with Python | * Base conversion with Python |
* <code>>>> hex(13) # Decimal to Hexadecimal conversion | * <code>>>> hex(13) # Decimal to Hexadecimal conversion |
'0xd' | '0xd' |
>>> hex(255) | >>> hex(253) |
'0xff' | '0xfd' |
>>> hex(256) | >>> hex(256) |
'0x100' | '0x100' |
>>> int('0x100', 16) # Hexadecimal to Decimal conversion | >>> int('0x100', 16) # Hexadecimal to Decimal conversion |
256 | 256 |
>>> int('11', 2) | |
3 | |
>>> int('1111', 2) # Binary to Decimal conversion | >>> int('1111', 2) # Binary to Decimal conversion |
15 | 15 |
>>> int('11111101', 2) | >>> int('11111101', 2) # '11111101' <=> '1111 1101' <=> 'FD' <=> 15 * 16 + 13 = 253 |
253 | |
>>> 15 * 16 + 13 | |
253 | 253 |
>>> 013 # DANGER! Python considers an integer to be in OCTAL base if it starts with a 0 | >>> 013 # DANGER! Python considers an integer to be in OCTAL base if it starts with a 0 |
>>> int('13', 8) # 1*8 + 3 | >>> int('13', 8) # 1*8 + 3 |
11</code> | 11</code> |
| |
| * More technical topics |
| * [[https://en.wikipedia.org/wiki/Bit_numbering|Bit numbering]]: the art of ordering bits, everything about MSB (Most Significant Byte) and LSB (Least Significant Byte) |
| * [[https://en.wikipedia.org/wiki/Endianness|Endianness]]: the art of ordering bytes |
==== Numerical values ==== | ==== Numerical values ==== |
| |