Difference between revisions of "Intermediate C++ Game Programming Tutorial 23"
From Chilipedia
(→Video Timestamp Index) |
(→Video Timestamp Index) |
||
Line 11: | Line 11: | ||
== Video Timestamp Index == | == Video Timestamp Index == | ||
[https://youtu.be/WPMwq0qM8Kk Tutorial 23] | [https://youtu.be/WPMwq0qM8Kk Tutorial 23] | ||
+ | <div class="mw-collapsible mw-collapsed"><br /> | ||
* Introduce <code>std::string ToBin(unsigned int n, int min_digits=0);</code> to convert integers to their binary representation [https://youtu.be/WPMwq0qM8Kk?t=0m15s 0:15] | * Introduce <code>std::string ToBin(unsigned int n, int min_digits=0);</code> to convert integers to their binary representation [https://youtu.be/WPMwq0qM8Kk?t=0m15s 0:15] | ||
* Bitwise operators [https://youtu.be/WPMwq0qM8Kk?t=1m50s 1:50] | * Bitwise operators [https://youtu.be/WPMwq0qM8Kk?t=1m50s 1:50] | ||
+ | <div class="mw-collapsible-content"> | ||
** Biswise AND (<code>&</code>): 1010 & 1100 = 1000, also called a mask | ** Biswise AND (<code>&</code>): 1010 & 1100 = 1000, also called a mask | ||
** Biswise OR (<code>|</code>): 1010 | 1100 = 1110, also called a mash | ** Biswise OR (<code>|</code>): 1010 | 1100 = 1110, also called a mash | ||
+ | </div> | ||
* Shifting operations [https://youtu.be/WPMwq0qM8Kk?t=5m10s 5:10] | * Shifting operations [https://youtu.be/WPMwq0qM8Kk?t=5m10s 5:10] | ||
+ | <div class="mw-collapsible-content"> | ||
** Shift left (<code><<</code>): 0011 << 1 = 0110, shifts binary zeros in from the right side into the lower order bit position | ** Shift left (<code><<</code>): 0011 << 1 = 0110, shifts binary zeros in from the right side into the lower order bit position | ||
** Shift right (<code>>></code>): 0110 >> 2 = 0001, shifts binary zeros in from the left side into the highter order bit position | ** Shift right (<code>>></code>): 0110 >> 2 = 0001, shifts binary zeros in from the left side into the highter order bit position | ||
** Except for negative integers: <code>(-1)>>n</code>: ...1111>>2 = ...1111, shifts binary ones in from the left | ** Except for negative integers: <code>(-1)>>n</code>: ...1111>>2 = ...1111, shifts binary ones in from the left | ||
+ | </div> | ||
* Packing and unpacking data [https://youtu.be/WPMwq0qM8Kk?t=7m07s 7:07] | * Packing and unpacking data [https://youtu.be/WPMwq0qM8Kk?t=7m07s 7:07] | ||
+ | <div class="mw-collapsible-content"> | ||
** Example: combining four 8-bit numbers into one 32-bit number by using <code><<</code> and <code>|</code> | ** Example: combining four 8-bit numbers into one 32-bit number by using <code><<</code> and <code>|</code> | ||
** Example: unpacking a 32-bit number to an 8 bit sequence by using <code>&</code> and <code>>></code> | ** Example: unpacking a 32-bit number to an 8 bit sequence by using <code>&</code> and <code>>></code> | ||
+ | </div> | ||
* Example use case: The <code>Color</code> class in the Framework [https://youtu.be/WPMwq0qM8Kk?t=12m20s 12:20] | * Example use case: The <code>Color</code> class in the Framework [https://youtu.be/WPMwq0qM8Kk?t=12m20s 12:20] | ||
+ | <div class="mw-collapsible-content"> | ||
** XRGB 8-bit values are packed into a 32-bit word using <code>(x<<24u) | (r<<16u) | (g<<8u) | b)</code> | ** XRGB 8-bit values are packed into a 32-bit word using <code>(x<<24u) | (r<<16u) | (g<<8u) | b)</code> | ||
+ | </div> | ||
* Packing flag values into single integer parameter [https://youtu.be/WPMwq0qM8Kk?t=14m29s 14:29] | * Packing flag values into single integer parameter [https://youtu.be/WPMwq0qM8Kk?t=14m29s 14:29] | ||
+ | <div class="mw-collapsible-content"> | ||
** Used in libraries such as the Windows API, DirectX, in STL | ** Used in libraries such as the Windows API, DirectX, in STL | ||
** Each value is represented by a single bit set in their binary representation, e.g. <code>enum Options { option1 = 0b01, option2 = 0b10 };</code> | ** Each value is represented by a single bit set in their binary representation, e.g. <code>enum Options { option1 = 0b01, option2 = 0b10 };</code> | ||
** Check for options with Bitwise AND: if you have an <code>int options</code>, check the option flag using <code>if( options & option1 )</code> | ** Check for options with Bitwise AND: if you have an <code>int options</code>, check the option flag using <code>if( options & option1 )</code> | ||
** Combine options with Bitwise OR: make an <code>int options = option1 | option3;</code> | ** Combine options with Bitwise OR: make an <code>int options = option1 | option3;</code> | ||
+ | </div> | ||
* Advanced Bitwise operations [https://youtu.be/WPMwq0qM8Kk?t=16m48s 16:48] | * Advanced Bitwise operations [https://youtu.be/WPMwq0qM8Kk?t=16m48s 16:48] | ||
* WORK-IN-PROGRESS | * WORK-IN-PROGRESS | ||
+ | </div> | ||
== Homework == | == Homework == |
Revision as of 06:49, 26 November 2019
Bit twiddling is a lot of bullshit that is usually avoided when possible because it leads to ass code. Still, it is necessary in various situations, so you gotta learn this shit, and now is as good a time as any I guess.
Topics Covered
- Bitwise AND (&), OR (|), and shift left/right (>> and <<)
- Unary bitwise NOT/complement (~) and bitwise XOR (^)
- Masking with & and combining with |
- Packing and unpacking smaller data in larger types
- Bit flags
- Bitwise arithmetic optimizations
Video Timestamp Index
- Introduce
std::string ToBin(unsigned int n, int min_digits=0);
to convert integers to their binary representation 0:15 - Bitwise operators 1:50
- Biswise AND (
&
): 1010 & 1100 = 1000, also called a mask - Biswise OR (
|
): 1010 | 1100 = 1110, also called a mash
- Biswise AND (
- Shifting operations 5:10
- Shift left (
<<
): 0011 << 1 = 0110, shifts binary zeros in from the right side into the lower order bit position - Shift right (
>>
): 0110 >> 2 = 0001, shifts binary zeros in from the left side into the highter order bit position - Except for negative integers:
(-1)>>n
: ...1111>>2 = ...1111, shifts binary ones in from the left
- Shift left (
- Packing and unpacking data 7:07
- Example: combining four 8-bit numbers into one 32-bit number by using
<<
and|
- Example: unpacking a 32-bit number to an 8 bit sequence by using
&
and>>
- Example: combining four 8-bit numbers into one 32-bit number by using
- Example use case: The
Color
class in the Framework 12:20
- XRGB 8-bit values are packed into a 32-bit word using
(x<<24u) | (r<<16u) | (g<<8u) | b)
- XRGB 8-bit values are packed into a 32-bit word using
- Packing flag values into single integer parameter 14:29
- Used in libraries such as the Windows API, DirectX, in STL
- Each value is represented by a single bit set in their binary representation, e.g.
enum Options { option1 = 0b01, option2 = 0b10 };
- Check for options with Bitwise AND: if you have an
int options
, check the option flag usingif( options & option1 )
- Combine options with Bitwise OR: make an
int options = option1 | option3;
- Advanced Bitwise operations 16:48
- WORK-IN-PROGRESS
Homework
- Figure out how the binary string formatter routine works
- Figure out why
x % 16
compiles tox & 0xF
The solution for the problems will not have its own video. Feel free to come on the discord and compare your answers with others, or to ask specific questions if you are having trouble understanding.