Skip to content

Instantly share code, notes, and snippets.

View engelmarkus's full-sized avatar

Markus Engel engelmarkus

View GitHub Profile
@engelmarkus
engelmarkus / ring_buffer.cpp
Created April 21, 2016 00:04
Example implementation for a simple ring buffer.
#include <array>
#include <cstdint>
#include <iostream>
#include <stdexcept>
using namespace std;
template <unsigned long long n>
constexpr auto count_set_bits = (n & 1) + count_set_bits<(n >> 1)>;
@engelmarkus
engelmarkus / to_bin.cpp
Created April 20, 2016 23:21
Convert a compile-time constant to a char-array containing the binary representation.
#include <iostream>
template <unsigned long long N, char... C>
constexpr auto to_bin = to_bin<(N >> 1), '0' + (N & 1), C...>;
template <char... C>
constexpr char to_bin<0, C...>[] = { C..., '\0' };
template <unsigned long long N, size_t L, char... C>
@engelmarkus
engelmarkus / coroutine.cpp
Created March 21, 2016 03:02
Using a boost coroutine for lazily generating an endless sequence of numbers.
#include <algorithm>
#include <iostream>
#include <vector>
#include <boost/asio/coroutine.hpp>
#include <boost/asio/yield.hpp>
using namespace std;
using namespace boost::asio;
@engelmarkus
engelmarkus / fold.cpp
Created March 21, 2016 01:48
Test of C++ fold expressions.
#include <iostream>
using namespace std;
constexpr auto all(auto&&... args) {
return (args && ...);
}
int main() {
cout << boolalpha;
@engelmarkus
engelmarkus / AsmTest.java
Created March 15, 2016 00:11
Calling an assembly function from Java
public class AsmTest {
public static native int addThem(int a, int b);
static {
// Looks for libaddThem.so or addThem.dll, depending on the OS
System.loadLibrary("addThem");
}
public static void main(String[] args) {
int result = addThem(4, 5);
@engelmarkus
engelmarkus / arp.asm
Created March 15, 2016 00:04
Sending an ARP packet using x86 assembly
; Just for fun.
; Compile with
; nasm -f elf32 -o arp.o arp.asm
; gcc -m32 -o arp arp.o
; Run it
; sudo ./arp
BITS 32
SEGMENT .data