Skip to content

Instantly share code, notes, and snippets.

@antoni
antoni / union_endianness.c
Created July 9, 2015 20:16
Detecting endianness at runtime
/*
* Source:
* http://stackoverflow.com/a/1001373/963881
*/
int is_big_endian(void)
{
union {
uint32_t i;
char c[4];
@antoni
antoni / clang-format_config.md
Last active August 30, 2015 08:08 — forked from Uflex/clang-format_config.md
Configuration of clang-format for QtCreator

Clang-format in QtCreator

Edit: QtCreator 3.1 introduced a new plugin called Beautifier that supports clang-format. You still have to install the clang tools separately but you don't have to add them as external tools anymore. The plugin can be configured in the options dialog under the beautifier tab > Clang Format. Add a new style and copy the configuration below without the curly braces.

QtCreator doesn't allow using clang-format by default. Watch QTCREATORBUG-10111 for a better integration. As of today, you should use it as an external tool, either replacing your selection or the entire file. If you choose to replace the file, you will have to save it before launching the tool and QtCreator will reload the file, making it impossible to undo/redo afterwards. If you choose to replace your selection, you often have to select the whole function, otherwise you will lose the indentation; clang-format doesn't seem to k

@antoni
antoni / hide_keystrokes.c
Created September 17, 2015 11:21
Hide & show keystrokes in a Linux terminal
#include <termios.h>
void hide_keystrokes() {
struct termios tty;
tcgetattr(STDIN_FILENO, &tty);
tty.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &tty);
}
void show_keystrokes() {
@antoni
antoni / error_handling.cu
Last active September 24, 2015 15:22
Error handling in CUDA
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
{
if (code != cudaSuccess)
{
fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) exit(code);
}
}
@antoni
antoni / FileLineReader.java
Last active October 30, 2015 10:16
Different ways of reading file line by line in Java (Java 8 on the bottom)
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
@antoni
antoni / AcademicDegree.java
Last active June 25, 2021 12:28
Random enum value in Java
public enum AcademicDegree {
DEGREE_MSC, DEGREE_PHD, DEGREE_PHD_HAB, DEGREE_PROF;
private static final AcademicDegree[] VALUES = values();
private static final int SIZE = VALUES.length;
private static final Random RANDOM = new Random();
public static AcademicDegree getRandomLetter() {
return VALUES[RANDOM.nextInt(SIZE)];
@antoni
antoni / c_timing.c
Created December 4, 2015 12:49
Timing C code
#include <time.h>
#include <stdio.h>
#include <stdint.h>
inline uint64_t rdtsc() {
uint32_t lo, hi;
__asm__ __volatile__ (
"xorl %%eax, %%eax\n"
"cpuid\n"
@antoni
antoni / ReflectionInvoke.java
Last active December 13, 2015 22:05
Invoke a Java method when given the method name as a string
// Get the method
try {
java.lang.reflect.Method method = obj.getClass().getMethod(methodName, param1.class, param2.class, ..);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
// Invoke the method
@antoni
antoni / FastIO.java
Created January 25, 2016 12:56
Fast I/O in Java
import java.io.*;
import java.util.InputMismatchException;
/**
* Created by Shreyans on $DATE at $TIME using IntelliJ IDEA (Fast IO Template)
*/
class $NAME
{
public static void main(String[] args) throws Exception
@antoni
antoni / catch.hpp
Created January 28, 2016 11:26
Single-header Catch (framework for unit-tests, TDD and BDD)
/*
* Catch v1.3.3
* Generated: 2016-01-22 07:51:36.661106
* ----------------------------------------------------------
* This file has been merged from multiple headers. Please don't edit it directly
* Copyright (c) 2012 Two Blue Cubes Ltd. All rights reserved.
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/