Some proofs for the Pythagorean Theorem:
http://www.cut-the-knot.org/pythagoras/index.shtmlThursday, May 6, 2010
A Nice Visual way to prove some mathematical formulas
Here is a link to the BLOG by Bill The Lizard describing six visual proofs for some mathematical Algebra formulas:
Some other interesting links, i found:
Some other visual puzzles:
Wednesday, May 5, 2010
Swapping values between two variables without a third variable
Here are two ways in which we can swap the values between two variables without the help of any third(temporary) variable:
-> Using method of Addition:
int main(void)
{
unsigned char A, B;
A = 39;
B = 18;
printf("Before:\tA: %d\tB: %d\n", A, B);
/* Swapping Algorithm */
A = A+B;
printf("A: %d\tB: %d\n", A, B);
B = A-B;
printf("A: %d\tB: %d\n", A, B);
A = A-B;
printf("After:\tA: %d\tB: %d\n", A, B);
return 0;
}
-> Using the method of EXOR:
int main(void)
{
unsigned char A, B;
A = 39;
B = 18;
printf("Before:\tA: %d\tB: %d\n", A, B);
/* Swapping Algorithm */
A ^= B;
printf("A: %d\tB: %d\n", A, B);
B ^= A;
printf("A: %d\tB: %d\n", A, B);
A ^= B;
printf("After:\tA: %d\tB: %d\n", A, B);
return 0;
}
=> NOTE: The maximum range of the value to be stored in the variables A & B should be such that, the value of (A+B) will be inside the maximum range of the data type defined for A & B variables.
=> NOTE: The method of EXOR will not work if a and b both have same values. If a and b both have the same values, then both will get cleared (equal to zero) after swapping.
Testing Endianness Of A Platform
A small code to find the endianness of the current platform for which the code will be compiled & where the executable from the code will be run:
#include
#define BIG_ENDIAN 0
#define LITTLE_ENDIAN 1
int TestByteOrder(void);
int TestByteOrder(void)
{
short int word = 0x0001;
char *byte = (char *) &word;
return (byte[0] ? LITTLE_ENDIAN : BIG_ENDIAN);
}
int main(void)
{
printf("The Endianness of the platform is: %s\n", TestByteOrder() ? "LITTLE Endian" : "BIG Endian");
return 0;
}
Pre-Increment and Post-Increment Operators Comparison
We will discuss the functionality and speed and property of the pre-increment and post-increment operators by comparing them with small programs:
#include
int main(void) {
int var1 = 5;
int var2 = 5;
int var3 = 5;
int var4 = 5;
printf("var1: %d\n", var1);
printf("var2: %d\n", var2);
printf("var3: %d\n", var3);
printf("var4: %d\n", var4);
printf("\n");
printf("++var1: %d\n", ++var1);
printf("var2++: %d\n", var2++);
printf("PreIncrement(var3): %d\n", PreIncrement(&var3));
printf("PostIncrement(var4): %d\n", PostIncrement(&var4));
printf("\n");
printf("var1: %d\n", var1);
printf("var2: %d\n", var2);
printf("var3: %d\n", var3);
printf("var4: %d\n", var4);
return 0;
}
int PreIncrement(int *a) {
*a = *a + 1;
return *a;
}
int PostIncrement(int *a) {
int temp = *a;
*a = *a + 1;
return temp;
}
The Output will be:
var1: 5
var2: 5
var3: 5
var4: 5
++var1: 6
var2++: 5
PreIncrement(var3): 6
PostIncrement(var4): 5
var1: 6
var2: 6
var3: 6
var4: 6
The following is a description collected from:
The two forms of the increment operator are called the prefix form (++x) and the
postfix form (x++). Which form should you use? Actually in C your choice doesn't
matter. However, if you use C++ with its overloadable operators, the prefix version
(++x) is more efficient. So, to develop good habits for learning C++, use the prefix
form.
Friday, April 30, 2010
Why do a lot of #defines in the kernel use do { ... } while(0)?
Question: Why do a lot of #defines in the kernel use do { ... } while(0)?
Answer:
There are a couple of reasons:
• Empty statements give a warning from the compiler so this is why
you see #define FOO do { } while(0).
• It gives you a basic block in which to declare local variables.
• It allows you to use more complex macros in conditional code.
Imagine a macro of several lines of code like:
#define FOO(x) \
printf("arg is %s\n", x); \
do_something_useful(x);
Now imagine using it like:
if (blah == 2)
FOO(blah);
This interprets to:
if (blah == 2)
printf("arg is %s\n", blah);
do_something_useful(blah);;
As you can see, the if then only encompasses the printf(), and the do_something_useful()
call is unconditional (not within the scope of the if), like you wanted it. So, by using a
block like do { ... } while(0), you would get this:
if (blah == 2)
do {
printf("arg is %s\n", blah);
do_something_useful(blah);
} while (0);
Which is exactly what you want.
•Another example:
#define exch(x,y) { int tmp; tmp=x; x=y; y=tmp; }
However that wouldn't work in some cases. The following code is meant to be an ifstatement
with two branches:
if (x > y)
exch(x,y); // Branch 1
else
do_something(); // Branch 2
But it would be interpreted as an if-statement with only one branch:
if (x > y) { // Single-branch if-statement!!!
int tmp; // The one and only branch consists
tmp = x; // of the block.
x = y;
y = tmp;
}
; // empty statement
else // ERROR!!! "parse error before else"
do_something();
The problem is the semi-colon (;) coming directly after the block. The solution for this
is to sandwich the block between do and while (0). Then we have a single
statement with the capabilities of a block, but not considered as being a block statement
by the compiler. Our if-statement now becomes:
if (x > y)
do {
int tmp;
tmp = x;
x = y;
y = tmp;
} while(0);
else
do_something();
Bye...
Umakanta
Subscribe to:
Posts (Atom)