«Consider yourself Perfectible makes you Perfect!»
Posts tagged c
Swap the value of two integers without temporary storage
Jan 13th
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 = AA ^ 1 = ~AA ^ A = 0
So, you can see how it get’s applied here:
#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.
Count bits set in parallel
Jan 7th
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
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)
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.
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. More >
Find the non repeating char in O(n) time and O(1) space – v2
Dec 18th
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
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 0×1 and 0×2 (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″.
Before I show you the code
Again, this comes from Luca Colantonio (http://uk.linkedin.com/in/lucacolantonio), smart ass that is too lazy to maintain a blog and post it himself (or even implementing himself – he just explained to me at work and I had to code it). Thanks Luca
Now, the code.
More >
Il VERO fattore WOW!!!
Jan 31st
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
Inutile che mi dilunghi, lui ha già scritto abbastanza e in maniera esauriente. Vi rimando al suo blog.
