- The
|
operator or theunion()
method is used to merge two sets and automatically handle duplicates.
Here’s a quick breakdown:
# merging, will ignore the duplicates
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1 | set2 # or set1.union(set2)
print(union_set) # Output: {1, 2, 3, 4, 5}
- The
|
operator is known as the "bitwise OR" operator in many programming languages. When used with sets in Python, it performs a union operation, combining all unique elements from both sets.
✋In the context of bitwise operations, | operates on the individual bits of integer values, performing a bitwise OR. However, when applied to sets, it is used to compute the union of those sets.
🔴 NOT TO CONFUSE with the ||
OR operator from javascript, that one is used for boolean
logic, evaluating to true if at least one of its operands is true
.
-
- For non-boolean values, it returns the first truthy value or the last value if none are truthy.
🟣 QUESTION: so it s not common in frontend applications (that doesn't involve bits or math opeartions)?
Correct, the bitwise OR operator (|)
is not commonly used in typical frontend applications that focus on UI design, user interactions, or animations. For most frontend development tasks, you won't find much use for bitwise operations unless you're dealing with specific cases where bit-level manipulation is beneficial. Bit Manipulation 01: Introduction
-
- If you have a need to manage multiple boolean options compactly (e.g., feature flags), you might use bitwise operations to set, check, or toggle these flags efficiently.
-
- In cases where you're working with custom graphics rendering (like using WebGL 🌈) or game development, you might use bitwise operations for pixel manipulation or to handle various states or flags in a compact way.
-
-
- In graphics programming, you might need to manipulate individual pixels of an image.
-
- Bitwise operations can be used for tasks like encoding color information into a single integer value, which is often useful in custom rendering engines or games.
🟣 when you say: "In graphics programming, you might need to 'manipulate' individual "pixels" of an image, DO you mean: manipulating the pixels in a RPG game and if yes, show me an example
- Absolutely! In RPG (Role-Playing Game) development or any other game where custom graphics are involved, manipulating individual pixels of an image can be essential. This might be done for a variety of reasons, such as applying custom effects, changing textures dynamically, or creating visual feedback.
https://youtube.com/shorts/0cYjreg7dpg?si=ZhnVae39pacE3OkB
-
- For highly optimized code where you need to perform operations at the bit level for performance reasons, bitwise operations might come into play. However, such scenarios are more specialized and less common in standard frontend development.
-
- operating systems, drivers, or embedded systems where you manipulate hardware registers or perform efficient data encoding/decoding.
-
- Techniques for compressing or encoding data efficiently often use bitwise operations.
-
- Some network protocols use bitwise operations for efficient data transmission and error checking. In summary, while bitwise operations, including the bitwise OR operator, are powerful tools, they are not typically required in most frontend development tasks unless you have specific needs for bit-level manipulation or optimization.