Skip to content

Instantly share code, notes, and snippets.

@dgreceanu
dgreceanu / quiz_polymorphic_for_stream.cpp
Created August 12, 2013 09:35
This is a way to get polymorphic behaviour for operator <<.
#include <iostream>
struct A
{
virtual std::ostream& put(std::ostream& o ) const { return o << 'A'; };
};
struct B : A
{
virtual std::ostream& put(std::ostream& o ) const { return o << 'B'; };
@dgreceanu
dgreceanu / ipow.c
Created June 19, 2013 17:54 — forked from orlp/ipow.c
Compute very fast power of a numer
int64_t ipow(int32_t base, uint8_t exp) {
static const uint8_t highest_bit_set[] = {
0, 1, 2, 2, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 255, // anything past 63 is a guaranteed overflow with base > 1