Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions concepts/bitwise-operators/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ See the section below for details.
In decimal representation, we distinguish positive and negative numbers by using a `+` or `-` sign to the left of the digits.
Using these symbols at a binary level proved inefficient for digital computing and raised the problem that `+0` is not the same as `-0`.

Rather than using `-` and `+`, all modern computers use a [`twos-complement`][twos-complement] representation for negative numbers, right down to the silicon chip level.
Rather than using `-` and `+`, all modern computers use a [`two's complement`][twos-complement] representation for negative numbers, right down to the silicon chip level.
This means that all bits are inverted and a number is _**interpreted as negative**_ if the left-most bit (also termed the "most significant bit", or MSB) is a `1`.
Positive numbers have an MSB of `0`.
This representation has the advantage of only having one version of zero, so that the programmer doesn't have to manage `-0` and `+0`.
Expand Down Expand Up @@ -145,21 +145,21 @@ This is **not** the `0b10011001` we would see in languages with fixed-size integ
The `~` operator only works as expected with _**unsigned**_ byte or integer types, or with fixed-sized integer types.
These numeric types are supported in third-party packages such as [`NumPy`][numpy], [`pandas`][pandas], and [`sympy`][sympy] but not in core Python.

In practice, Python programmers quite often use the shift operators described below and `& | ^` with positive numbers only.
In practice, Python programmers quite often use `&`, `|`, `^`, and the shift operators described below with positive numbers only.
Bitwise operations with negative numbers are much less common.
One technique is to add [`2**32 (or 1 << 32)`][unsigned-int-python] to a negative value to make an `int` unsigned, but this gets difficult to manage.
Another strategy is to work with the [`ctypes`][ctypes-module] module, and use c-style integer types, but this is equally unwieldy.


## [`Shift operators`][bitwise-shift-operators]

The left-shift operator `x << y` simply moves all the bits in `x` by `y` places to the left, filling the new gaps with zeros.
Note that this is arithmetically identical to multiplying a number by `2**y`.
The left-shift operator `x << y` moves all the bits in `x` by `y` places to the left, filling the new gaps with zeros.
Note that this is arithmetically identical to multiplying a number by `(2**y)`.

The right-shift operator `x >> y` does the opposite.
This is arithmetically identical to integer division `x // 2**y`.
This is arithmetically identical to integer division `x // (2**y)`.

Keep in mind the previous section on negative numbers and their pitfalls when shifting.
Keep in mind the previous section on negative numbers and their pitfalls when shifting them in Python.


```python
Expand Down Expand Up @@ -191,7 +191,7 @@ Keep in mind the previous section on negative numbers and their pitfalls when sh
[symmetric-difference]: https://math.stackexchange.com/questions/84184/relation-between-xor-and-symmetric-difference#:~:text=It%20is%20the%20same%20thing,they%20are%20indeed%20the%20same.
[sympy]: https://docs.sympy.org/latest/modules/codegen.html#predefined-types
[tilde]: https://en.wikipedia.org/wiki/Tilde
[twos-complement]: https://en.wikipedia.org/wiki/Two%27s_complement#:~:text=Two's%20complement%20is%20the%20most,number%20is%20positive%20or%20negative.
[twos-complement]: https://en.wikipedia.org/wiki/Two%27s_complement
[unsigned-int-python]: https://stackoverflow.com/a/20768199
[xor-cipher]: https://en.wikipedia.org/wiki/XOR_cipher
[xor]: https://stackoverflow.com/a/2451393
6 changes: 3 additions & 3 deletions concepts/bitwise-operators/introduction.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# Introduction

Down at the hardware level, transistors can only be on or off: two states that we traditionally represent with `1` and `0`.
Down at the hardware level, [transistors can only be on or off][how-transistors-work]: two states that we traditionally represent with `1` and `0`.
These are the [`binary digits`][binary-digits], abbreviated as [`bits`][bits].
Awareness of `bits` and `binary` is particularly important for systems programmers working in low-level languages.

However, for most of the history of computing the programming priority has been to find increasingly sophisticated ways to _abstract away_ this binary reality.


In Python (and many other [high-level programming languages][high-level-language]), we work with `int`, `float`, `string` and other defined _types_, up to and including audio and video formats.
We let the Python internals take care of (eventually) translating everything to bits.
Python internals take care of (_eventually_) translating all the higher-level data to bits.


Nevertheless, using [bitwise-operators][python-bitwise-operators] and [bitwise operations][python-bitwise-operations] can sometimes have significant advantages in speed and memory efficiency, even in a high-level language like Python.

[high-level-language]: https://en.wikipedia.org/wiki/High-level_programming_language
[how-transistors-work]: https://www.build-electronic-circuits.com/how-transistors-work/
[binary-digits]: https://www.khanacademy.org/computing/computers-and-internet/xcae6f4a7ff015e7d:digital-information/xcae6f4a7ff015e7d:binary-numbers/v/the-binary-number-system
[bits]: https://en.wikipedia.org/wiki/Bit
[python-bitwise-operations]: https://docs.python.org/3/reference/expressions.html#binary-bitwise-operations
Expand Down
4 changes: 0 additions & 4 deletions concepts/bitwise-operators/links.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
[
{
"url": "https://wiki.python.org/moin/BitwiseOperators/",
"description": "BitwiseOperators on the Python wiki."
},
{
"url": "https://realpython.com/python-bitwise-operators",
"description": "Real Python: Bitwise Operators in Python."
Expand Down
12 changes: 6 additions & 6 deletions concepts/complex-numbers/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
`Complex numbers` are not complicated.
They just need a less alarming name.

They are so useful, especially in engineering and science, that Python includes [`complex`][complex] as a standard numeric type alongside integers ([`int`s][ints]) and floating-point numbers ([`float`s][floats]).
They are so usefulespecially in engineering and sciencethat Python includes [`complex`][complex] as a standard numeric type, alongside integers ([`int`s][ints]) and floating-point numbers ([`float`s][floats]).


## Basics
Expand Down Expand Up @@ -143,7 +143,7 @@ Any [mathematical][math-complex] or [electrical engineering][engineering-complex
Alternatively, Exercism has a `Complex Numbers` practice exercise where you can implement a complex number class with these operations from first principles.


Integer division is ___not___ possible on complex numbers, so the `//` and `%` operators and `divmod()` functions will fail for the complex number type.
Integer division is ___not___ possible on complex numbers, so the `//` and `%` operators and the `divmod()` function will fail for the complex number type.


There are two functions implemented for numeric types that are very useful when working with complex numbers:
Expand Down Expand Up @@ -235,13 +235,13 @@ If you are reading this on any sort of screen, you are utterly dependent on some

1. __Semiconductor chips__.
- These make no sense in classical physics and can only be explained (and designed) by quantum mechanics (QM).
- In QM, everything is complex-valued by definition. (_its waveforms all the way down_)
- In QM, everything is complex-valued by definition. (_it's waveforms all the way down_)

2. __The Fast Fourier Transform algorithm__.
2. __The Fast Fourier Transform (FFT) algorithm__.
- FFT is an application of complex numbers, and it is in _everything_ connected to sound transmission, audio processing, photos, and video.

-MP3 and other audio formats use FFT for compression, ensuring more audio can fit within a smaller storage space.
- JPEG compression and MP4 video, among many other image and video formats also use FTT for compression.
- MP3 and other audio formats use FFT for compression, ensuring more audio can fit within a smaller storage space.
- JPEG compression and MP4 video, among many other image and video formats, also use FTT for compression.

- FFT is also deployed in the digital filters that allow cellphone towers to separate your personal cell signal from everyone else's.

Expand Down
8 changes: 5 additions & 3 deletions concepts/complex-numbers/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
`Complex numbers` are not complicated.
They just need a less alarming name.

They are so useful, especially in engineering and science (_everything from JPEG compression to quantum mechanics_), that Python includes [`complex`][complex] as a standard numeric type alongside integers ([`int`s][ints]) and floating-point numbers ([`float`s][floats]).

They are so useful — especially in engineering and science — that Python includes [`complex`][complex] as a standard numeric type, alongside integers ([`int`s][ints]) and floating-point numbers ([`float`s][floats]).


A `complex` value in Python is essentially a pair of floating-point numbers:

Expand Down Expand Up @@ -74,13 +76,13 @@ There are two common ways to create complex numbers.

Most of the [`operators`][operators] used with floats and ints also work with complex numbers.

Integer division is _**not**_ possible on complex numbers, so the `//` and `%` operators and `divmod()` functions will fail for the complex number type.
Integer division is _**not**_ possible on complex numbers, so the `//` and `%` operators and the `divmod()` function will fail for the complex number type.

Explaining the rules for complex number multiplication and division is out of scope for this concept (_and you are unlikely to have to perform those operations "by hand" very often_).

Any [mathematical][math-complex] or [electrical engineering][engineering-complex] introduction to complex numbers will cover these scenarios, should you want to dig into the topic.

The Python standard library has a [`math`][math-module] module full of useful functionality for working with real numbers and the [`cmath`][cmath] module is its equivalent for working with complex numbers.
The Python standard library has a [`math`][math-module] module full of useful functionality for working with real numbers, and the [`cmath`][cmath] module is its equivalent for working with complex numbers.


[cmath]: https://docs.python.org/3/library/cmath.html
Expand Down
Loading