Skip to content

Instantly share code, notes, and snippets.

View bxt's full-sized avatar

Bernhard Häussner bxt

View GitHub Profile
@bxt
bxt / boolean2arithmetic.js
Created April 30, 2011 20:33
Boolean operators ! & | xor rewritten as arithmetic ones (+ * - on numeric input 0/1) in JS
/*
* Just for fun :) AND, OR and XOR with only "+ * - 2", plus "( ) 1" for NOT
*/
function not(a) {
return (a-1)*(a-1);
return a*(a-1)+(a+1)-2*a;
return (a-2)*a+1;
return and(a-1,a-1);
}
@bxt
bxt / array-pointr-arithmetic.cpp
Created May 27, 2011 11:50
Arrays access translates to pointer arithmetic in C/C++
int a[1];
a[0]=1;
if(0[a]==a[0]==1);
// a[b] == *(a+b) == *(b+a) == b[a]
@bxt
bxt / iceWMdual.mkd
Created July 18, 2011 17:15
IceWM Dual Screen expanding howto (very basic)

Step 1: Make sure xrandr works fine

Check your screen's names by typing:

xrandr -q

Step 2: Expand your screen

Then disable one of them: (notice VGA-1 is the name of the screen, will differ, e.g. HDMI-1 if the screen is connected via HDMI)

@bxt
bxt / README-PHP-Namespace-test-file.mkd
Created July 20, 2011 12:51
PHP Namespace test file

README for PHP Namespace test file

This is just a little script, which can tell you if your hoster/php verion supports namespaces. Upload to your htdocs/webroot and naviagte your browser to the page. It might tell you "Namespaces seem to work!" or something like "Parse error: syntax error, unexpected T_STRING in /var/www/httpdocs/nst.php on line 1"

@bxt
bxt / speedtest.out
Created September 3, 2011 12:25
Sed speed test -n option and printin vs negated adress vs quitting
02:16 bernhard@horst:~> time for i in {1..10000}; do sed -n -e '1p' edt.txt > /dev/null;done
real 1m23.424s
user 0m44.611s
sys 0m8.801s
02:18 bernhard@horst:~> time for i in {1..10000}; do sed -e '1!d' edt.txt > /dev/null;done
real 1m23.036s
user 0m45.827s
sys 0m10.609s
@bxt
bxt / RingBuffer.class.php
Created September 7, 2011 13:15
One-pointer Circular Buffer w/o overflow check and FIFO, using SplFixedArray
<?php
/**
* One-pointer Circular Buffer
*
* This comes without any overwrite
* checks or FIFO functionality.
* You'll need SPL.
*
* @author Bernhard Häussner
* @date 2011-09-07
@bxt
bxt / csv-export.sh
Created September 13, 2011 20:58
Exporting whole MySQL databse to CSV files
#!/bin/bash
# USAGE: Will create a .tar.gz with CSVs of all tables in schema.
# Configure below and run as root (i.e. the user mysql runs as)
#
# The script will (or should) SELECT * INTO OUTFILE your tables
# and save them into csv files under /tmp dir first, then name them
# like the tables and move them thogether into a directory. Then
# it will tar everything together and chown you the tarball.
# Schema to export:
@bxt
bxt / jquery-eventhacks.js
Created September 24, 2011 21:33
jQuery binding event handlers to non-DOM objects Javascipt code examples
// This is a little demo File on how to bind event listeners
// to own, non-DOM objects with jQuery.
// Since calling the default action is calling the identically
// named methods of our custom action and that would usually
// mean triggering the event again we use this handy callback:
function preventSelfcallCallback (event) {
event.preventDefault();
}
@bxt
bxt / percentage-mysql-avg.sql
Created September 29, 2011 17:57
MySQL - Calculate percentage of rows/records matching predicate using IF() and AVG
-- Tip: You can use IF() and AVG() together to get a percentage value
-- without doing subquerys
-- -
-- Basic usage:
-- SELECT AVG(IF( predicate ,100,0)) as percentage FROM table
-- Example:
-- -
SELECT
AVG(IF(f.mid,100,0)) as `percentage filed`
FROM
@bxt
bxt / filename-utils.js
Created October 12, 2011 20:46
Javascript batch processing filename utilities
/**
* Return something as filename-save string.
*
* Result will contain only 0-9, a-z and "-".
* It converts german umlauts, you might want
* to add some conversions for your target
* language.
*/
function filesc(str) {