Difference between revisions of "Intermediate C++ Game Programming Tutorial 23"

From Chilipedia
Jump to: navigation, search
(Video Timestamp Index)
(Video Timestamp Index)
Line 47: Line 47:
 
*:- Use case: a "configurable flipper". <code>a^0b11110000</code> will flip the 4 left bits of <code>a</code> and leave the 4 right bits the same
 
*:- Use case: a "configurable flipper". <code>a^0b11110000</code> will flip the 4 left bits of <code>a</code> and leave the 4 right bits the same
 
*:- Toggle an option: <code>options ^= option1;</code>
 
*:- Toggle an option: <code>options ^= option1;</code>
* Don't use Bitwise AND in logical operations <code>if ( a & b )</code> [https://youtu.be/WPMwq0qM8Kk?t=19m49s 19:49]
+
* Don't use Bitwise AND in logical operations like <code>if ( a & b )</code> [https://youtu.be/WPMwq0qM8Kk?t=19m49s 19:49]
 
** <code>bool(x)</code> evaluates to false if x==0, otherwise to true
 
** <code>bool(x)</code> evaluates to false if x==0, otherwise to true
 
** <code>bool( 0b01 && 0b10 )</code> is true, whereas <code>bool( 0b01 & ob10 )</code> is false
 
** <code>bool( 0b01 && 0b10 )</code> is true, whereas <code>bool( 0b01 & ob10 )</code> is false

Revision as of 06:11, 27 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

Tutorial 23

[Expand]
  • Converting integers to their binary representation 0:15
    • Introducing std::string ToBin(unsigned int n, int min_digits=0);
  • Bitwise operators AND (&), OR (|) 1:50
  • Shifting operations left (<<), right (>>) 5:10
  • Packing and unpacking data 7:07
  • Example use case: The Color class in the Framework 12:20
  • Packing flag values into a single integer parameter 14:29
  • Advanced Bitwise operators NOT (~), XOR (^) 16:48
    • Bitwise NOT (~a): unary operation that flips all the bits of a
    - Use case: switch off an option: options &= ~option1;
    • Bitwise XOR (^): binary operation sets each bit position to 1 only if the bits at that position of the two operands are different
    - Use case: a "configurable flipper". a^0b11110000 will flip the 4 left bits of a and leave the 4 right bits the same
    - Toggle an option: options ^= option1;
  • Don't use Bitwise AND in logical operations like if ( a & b ) 19:49
    • bool(x) evaluates to false if x==0, otherwise to true
    • bool( 0b01 && 0b10 ) is true, whereas bool( 0b01 & ob10 ) is false

Homework

  • Figure out how the binary string formatter routine works
  • Figure out why x % 16 compiles to x & 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.

See also