Skip to content

Instantly share code, notes, and snippets.

@tkihira
tkihira / max
Last active August 29, 2015 14:10
function max(a, b) {
// 差のある最上位のビットを立てる
// (例えば 11010010 と 10110101 の場合、 x は 01000000 )
var x = a ^ b;
x |= x >>> 1;
x |= x >>> 2;
x |= x >>> 4;
x |= x >>> 8;
x |= x >>> 16;
var h = x ^ (x >>> 1);
@tkihira
tkihira / meta.cpp
Created December 26, 2014 03:53
Meta-programming for ten puzzle
#include <stdio.h>
template<int a_bunshi, int a_bunbo>
struct Calc1 {
enum { result = (a_bunbo != 0 && a_bunbo * 10 == a_bunshi) };
};
template<int a_bunshi, int a_bunbo, int b_bunshi, int b_bunbo>
struct Calc2 {
enum {
// http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/
"use strict";
// Optional. You will see this name in eg. 'ps' or 'top' command
process.title = 'node-chat';
// Port where we'll run the websocket server
var webSocketsServerPort = 1337;
// websocket and http servers
@tkihira
tkihira / gist:1483c90f26eb92021306
Created May 27, 2015 03:05
1+23-4+56+7+8+9 = 100
#include <stdio.h>
void recursive(int level, int* op) {
if(level == 8) {
int result = 0;
int lastOp = 1;
int stack = 0;
for(int i = 0; i < 9; i++) {
int num = i + 1;
stack *= 10;
@tkihira
tkihira / gist:2161538
Created March 22, 2012 18:43
goto implementation on JavaScript
var gotoLabel;
do {
switch(gotoLabel) {
default:
/* code here... */
gotoLabel = "label1"; break; /*goto lable1*/
case "label1":
/* code here... */
gotoLable = null;
}
@tkihira
tkihira / profiler.js
Created March 24, 2012 16:00
JavaScript General Profiler
window.Profiler = {
stackFrame: [],
counters: {},
working: false,
listup: function(name, obj, parentName, parentObj, level) {
if(level > 10) {
return;
}
var fullName = parentName? parentName + "." + name: name;
@tkihira
tkihira / gist:2232412
Created March 29, 2012 01:57
test for cc
/**
* @constructor
*/
var f1 = function() {
};
/**
*
*/
f1.prototype.foo = function() {
@tkihira
tkihira / gist:2250405
Created March 30, 2012 09:46
for..in hack
var o = {a: 0, b: 1, c: 2, d: 3};
var a = []; var i = 0;
for(a[i++] in o);
@tkihira
tkihira / gist:2292457
Created April 3, 2012 14:32
Chrome's bug code
var canvas = document.createElement("canvas");
canvas.width = canvas.height = 300;
document.body.appendChild(canvas);
var ctx = canvas.getContext("2d");
var m = document.createElement("canvas");
m.width = m.height = 100;
var mctx = m.getContext("2d");
mctx.fillStyle = "#f00";
mctx.fillRect(0, 0, 100, 100);
@tkihira
tkihira / gist:2779981
Created May 24, 2012 07:08
VM sample
while(offset >= 0) {
switch(offset) {
case 0: // "PUSH 10"
stack.push(10);
case 2: // "PUSH 8"
stack.push(8);
case 4: // "LESS"
var a = stack.pop();
var b = stack.pop();
stack.push((a < b)? 1: 0);