This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
other:python:misc_by_jyp [2023/05/04 09:46] jypeter [Data representation] Added the Base notions section |
other:python:misc_by_jyp [2023/05/04 12:33] jypeter [Base notions] Improved |
||
---|---|---|---|
Line 470: | Line 470: | ||
==== 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//** | ||
Line 476: | Line 476: | ||
* 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 | ||
Line 502: | Line 498: | ||
>>> 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 ==== | ||