Tuesday, September 25, 2007

Optimizing C and C++ Code

Optimizing C and C++ Code

Embedded software often runs on processors with limited computation power, thus optimizing the code becomes a necessity. In this article we will explore the following optimization techniques for C and C++ code developed for Real-time and Embedded Systems.

1. Adjust structure sizes to power of two
2. Place case labels in narrow range
3. Place frequent case labels first
4. Break big switch statements into nested switches
5. Minimize local variables
6. Declare local variables in the inner most scope
7. Reduce the number of parameters
8. Use references for parameter passing and return value for types bigger than 4 bytes
9. Don't define a return value if not used
10. Consider locality of reference for code and data
11. Prefer int over char and short
12. Define lightweight constructors
13. Prefer initialization over assignment
14. Use constructor initialization lists
15. Do not declare "just in case" virtual functions
16. In-line 1 to 3 line functions

Al

read more @ http://www.eventhelix.com/RealtimeMantra/Basics/OptimizingCAndCPPCode.htm#Place%20Frequent%20Case%20Labels%20First

Wednesday, September 5, 2007

Programming puzzles ... dare to solve

Programming puzzles ..... these are quite boring for the masters and experts , neways have a look at it ... i guess it will be fun for new experts .... and posts the answers as comments without checking out provided forum post.

Q1 Write a "Hello World" program in 'C' without using a semicolon.
Q2 Write a C++ program without using any loop (if, for, while etc) to
print numbers from 1 to 100 and 100 to 1;
Q3 C/C++ : Exchange two numbers without using a temporary variable.
Q4 C/C++ : Find if the given number is a power of 2.
Q5 C/C++ : Multiply x by 7 without using multiplication (*) operator.
Q6 C/C++ : Write a function in different ways that will return f(7) =
4 and f(4) = 7
Q7 Remove duplicates in array
Q8 Finding if there is any loop inside linked list.
Q9 Remove duplicates in an no key access database without using an
array
Q10 Write a program whose printed output is an exact copy of the
source. Needless to say, merely echoing the actual source file is not
allowed.
Q11 From a 'pool' of numbers (four '1's, four '2's .... four '6's),
each player selects a number and adds it to the total. Once a number
is used, it must be removed from the pool. The winner is the person
whose number makes the total equal 31 exactly.
Q12 Swap two numbers without using a third variable.
Given an array (group) of numbers write all the possible sub groups of
this group.
Q14 Convert (integer) number in binary without loops.

Al

http://www.praveen.ws/journal/2006/09/04/c-and-c-puzzles/
http://coding.derkeiler.com/Archive/C_CPP/comp.lang.c/2004-06/2652.html
http://www.velocityreviews.com/forums/t284024-programming-puzzle.html

without semicolon how to write c program

How to print something - say to write a code "hello world" without any semicolon.
Well the solution is very simple...






#include < stdio.h >
main() { if (printf("Hello, world!")) {} }

Al


Monday, September 3, 2007

.getClass() vs instanceof

class Parent {}
class Child extends Parent {}

Child c = new Child();

c instanceof Parent is true
c.getClass().equals(Parent.class) is false


bye
Al (alokadvin)

mypictr - we make your profile picture

Hey ,
find a cool way to edit your pictures on-line

mypictr - we make your profile picture

Al

Sunday, September 2, 2007

Affine Cipher Deciphered

Hi

Affine Ciphers -
An encipherment scheme (or algorithm) of the form
E (x) = (ax + b) MOD 26
is called an affine cipher. Here x is the numerical equivalent of the given plaintext letter, and a and
b are (appropriately chosen) integers.

What are the possible values for which one to one ciphers are possible...
i.e if p and q are two plaintext such that p ne q then E(k,p) ne E(k,q).

The function E (x) = (ax + b) MOD 26 defines a valid affine cipher if a is relatively prime to 26, and b
is an integer between 0 and 25, inclusive. If b = 0, then we refer to this cipher as a decimation cipher.
(Note that since there are 12 valid choices of a and 26 valid choices of b, there are 12 × 26 = 312 possible
valid affine ciphers.)
Also note that if a = 1, then E (x) = (x + b) MOD 26 is simply a Caesar (+b) shift cipher.

Do more with affine cipher @ http://home.clara.net/paulwlocke/portfolio/affine.html

bye