Scope 'this', and scope 'that'

As a guy that tries to live his professional life in the middle of the dichotomy “C/C++ - JavaScript”, I often find myself discussing the discrepancies, differences and implementation details of the latter (an half-assed prototypal language) using the former. The other day I was chatting with Luca (@lucabox) about scope in JS, and we were describing the different situations you can end up with… and how we work our way out of those culprits. ...

February 12, 2012 Â· 8 min Â· 1541 words

Swap the value of two integers without temporary storage

Someone says this is an old lame trick. I think it’s simple and clever use of XOR. How/Why does it work? It’s built around the properties of the XOR ^ operator, who has the following properties: A ^ B = B ^ A (commutative) A ^ 0 = A A ^ 1 = ~A A ^ A = 0 So, you can see how it get’s applied here: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 #include <stdio .h> int main(void) { unsigned int a, b; // ... populate somehow "a" and "b"... printf("a = %d - b = %d\n", a, b); a ^= b; // store in "a" the value of "a XOR b" b ^= a; // store in "b" the value of "a XOR b XOR b" = "a XOR 0" = "a" a ^= b; // store in "a" the velue of "a XOR b XOR a" = "b XOR 0" = "b" printf("a = %d - b = %d\n", a, b); } Neat.

January 13, 2010 Â· 1 min Â· 174 words

Count bits set in parallel

This time it’s not something I make myself. Indeed, I still can’t “see” it 100%: I got it, but it’s a bit complex. A cute little lady counting (bits? ;-) ) It’s a method to count the number of bits in a number in O(1), in just 5 lines of code. INHUMAN. The “human” solutions Of course, there are methods that look way more easy and, given that the size of a number in memory is “fixed”, the O(1) still stands. For example: 0. Based on the “evenness/oddness” of the number 1 2 3 4 5 6 7 8 9 10 unsigned int bits_counter_v0(unsigned int x) { unsigned int count = 0; while ( x != 0 ) { // If odd, add 1 count += (x % 2 == 0) ? 0 : 1; x >>= 1; } return count; } 1. Counting one bit at a time (always the least significant one) 1 2 3 4 5 6 7 8 9 10 unsigned int bits_counter_v1(unsigned int x) { unsigned int count = 0; while ( x != 0 ) { // If least-significant bit is 1, add 1 count += (x & 1) ? 1 : 0; x >>= 1; } return count; } 2. Counting 4 bit at a time with max 8 shifts, using an “hashmap” with precalculated results The fact that it can count the bits in “max 8 shifts” has the trade off of the memory used by the hashmap. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 unsigned int bits_counter_v2(unsigned int x) { unsigned int count = 0; // "Hashmap" of the values for the least significant 4 bits unsigned int int_to_bits_count[16] = { 0, // 0 00 1, // 1 01 1, // 2 10 2, // 3 11 1, // 4 100 2, // 5 101 2, // 6 110 3, // 7 111 1, // 8 1000 2, // 9 1001 2, // 10 1010 3, // 11 1011 2, // 12 1100 3, // 13 1101 3, // 14 1110 4 // 15 1111 }; while ( x != 0 ) { // Add the bits count of the least significant 4 bits count += int_to_bits_count[ x & 15 ]; x >>= 4; } return count; } Let’s see what some insane people made. ...

January 7, 2010 Â· 7 min Â· 1357 words

Find the non repeating char in O(n) time and O(1) space - v2

My colleague and friend Luca (@lucabox) described a better solution to the problem of "Finding the first non repearing char in a string in O(n) time and O(1) space". It uses smartly the space, making the solution nicer and slicker. Or we are just 2 geeks that need to give more attention to their girlfriends :P Luca’s solution description The logic of this solution is based on the usage of an array of unsigned chars. Every char (assumed to be lowecase) has an associated small byte (1 char = 8 bits), where the bits 0x1 and 0x2 (the 2 least significant) represents, respectively, “present once in the input string” and “present multiple times in the input string”. After the input is “scanned” once, and every letter is marked with the correspondent “presence bit” (once, multiple or none), it get’s scanned a second time to find the first char of the input which has the bit “present once” set to “1”. ...

December 18, 2009 Â· 3 min Â· 503 words

Il VERO fattore WOW!!!

Pollycoke, che sta ormai acquistando una certa notorietà in tutta la comunità Italiana (ma non solo), ci parla di una proposta SCONVOLGENTE (in positivo) fatta dalla comunità degli sviluppatori del Kernel ai produttori di hardware: […] the Linux kernel community is offering all companies free Linux driver developmentAll that is needed is some kind of specification that describes how your device works, or the email address of an engineer that is willing to answer questions every once in a while. […] In return, you will receive a complete and working Linux driver that is added to the main Linux kernel source tree ...

January 31, 2007 Â· 1 min Â· 120 words

XGL: OpenGL sempre piĂą prepotentemente sui nostri Desktop

XGL: tutti ne stanno parlando ultimamente. Ci sono articoli in ogni dove su XGL ma… che é? Prendiamo da Wikipedia: Xgl is an X server architecture, started by David Reveman, layered on top of OpenGL via glitz. Some say that it is seen as the future of the X.Org Server, but some disagree because it requires a 3D graphics card. Although most PCs are nowadays shipped with such a card, most vendors (most notably NVIDIA and ATI Technologies) don’t provide open source drivers for their cards, which do not work on all computer architectures supported by the X.Org Server. ...

February 11, 2006 Â· 5 min Â· 898 words

Soon, Native GTK+ on MacOSX

Soon you may be able to execute Gnome/Gtk+ based programs NATIVELLY NATIVELY on Quartz without the X11 Emulation. Someone (thanks to exist) is working on the porting. It may be the incipit of a new era for Open Source software on MacOSX. Take a look at this. One of the most interesting characteristics of Gnome-oriented applications is the coherence between the HIG (Human Interface Guidelines) of Gnome and MacOSX. Source: Melablog.it and StyleMac.com.

January 31, 2006 Â· 1 min Â· 73 words

GTK+ howto from IBM DeveloperWorks

A collection of howto on Gtk+ on the rich DeveloperWorks website from IBM. 2 articles are available right now: Why use Gtk+? How to use Gtk+ Source, OSSBlog.

January 12, 2006 Â· 1 min Â· 28 words

Embedding Python in your C programs

The language of choice for large, high-performance applications in Linux is almost always C, or somewhat less often C++. Both are powerful languages that allow you to create high-performance natively compiled programs. However, they are not languages that lend themselves to runtime flexibility. Once a C/C++ application is compiled, its code is pretty much static. At times, that can be a real hindrance. For example, if you want to allow users of a program to create plugins easily that extend the application’s functionality, you have to deal with complex dynamic linking issues that can cause no end of headaches. Additionally, your users will have to know C/C++ in order to extend the application, which severely limits the number of people capable of writing extensions. ...

January 3, 2006 Â· 2 min Â· 252 words

Fish: a new Unix Shell

fish is a user friendly command line shell for UNIX-like operating systems such as Linux. Bash is a standard (de facto), as every *nix user now but… this is the beauty of the OpenSource: any other programmer can think “hey, I want to remake this in another way!” and, than, do! Fish is a beautiful example. For screenshots-addicted users (like me ;)), go here. -EDIT- Thanks to Nemo for “errata-corrige”.

December 20, 2005 Â· 1 min Â· 70 words