Hi

Found some useful tricks which will be useful during programming contest.

**Quickly divide or multiply by 2**

Numbers are stored in memory as bits. So bitwise operations are quite fast.

```
So if you shift all bits to the left, you are multiplying a number by 2:
cout<<(3 << 1); //6 //shift bits to the left one time
cout<<(3 << 2); //12
Similarly if you shift bits to the right, you will be dividing them by 2:
cout<<(12 >> 1); //6
```

**Swap two no without temporary.**

Method 1 :

```
a = a + b;
b = a - b;
a = a - b;
```

Method 2 :

```
a ^= b;
b ^= a;
a ^= b;
```

**Loop in C-string:**

```
char s[100];
for (int i = 0; s*; ++i) { ... }
Quite useful (also avoids the strlen usage, that you could forget is O(n) and put on for condition.)
```

**Testing if not negative 1:**

```
if (~x) { ... }
```

In competitive programming we look to code fast and try to write as little as possible, so a simple

```
x ! = -1
```

can be shortened to 2 characters.

Last one:

**Finding size of array without sizeof in c and c++**

```
int main ()
{
int arr[100];
printf ("%d
```

", (&arr)1 - arr);

return 0;

}

Source- link text and link text

Help me in adding more of these …