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

From Chilipedia
Jump to: navigation, search
(Video Timestamp Index)
(Video Timestamp Index)
Line 14: Line 14:
 
* Converting integers to their binary representations [https://youtu.be/WPMwq0qM8Kk?t=0m25s 0:25]
 
* Converting integers to their binary representations [https://youtu.be/WPMwq0qM8Kk?t=0m25s 0:25]
 
<div class="mw-collapsible-content">
 
<div class="mw-collapsible-content">
** Introducing <code>std::string ToBin(unsigned int n, int min_digits=0);</code>
+
:* Introducing <code>std::string ToBin(unsigned int n, int min_digits=0);</code>
 
::<syntaxhighlight lang="cpp" line>
 
::<syntaxhighlight lang="cpp" line>
struct EqVec2
+
std::string ToBin( unsigned int n, int min_digits = 0)
 
{
 
{
    template <typename T>
+
std::string bin_str;
    bool operator()( const T& lhs,const T& rhs ) const
+
for ( int count = 0; n!=0 || count < min_digits; n>>=1,count++ )
    {
+
{
        return (lhs.x == rhs.x) && (lhs.y == rhs.y);
+
bin_str.push_back( bool( n & 0b1 ) ? '1' : '0' );
    }
+
}
};
+
std::reverse( bin_str.begin(),bin_str.end() );
 +
return bin_str;
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 
</div>
 
</div>

Revision as of 06:15, 5 May 2020

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 representations 0:25
  • Bitwise operators AND (&), OR (|) 1:50
  • Shifting operations left (<<), right (>>) 5:10
  • Packing and unpacking data, masking with & and mashing/combining with | 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
  • Don't use Bitwise AND in logical operations like if ( a & b ) 19:49
  • Bitwise arithmetic optimizations 22:30

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