The stack is the memory set aside as scratch space for a thread of execution. When a function is called, a block is reserved on the top of the stack for local variables and some bookkeeping data. When that function returns, the block becomes unused and can be used the next time a function is called. The stack is always reserved in a LIFO (last in first out) order; the most recently reserved block is always the next block to be freed. This makes it really simple to keep track of the stack; freeing a block from the stack is nothing more than adjusting one pointer.

The heap is memory set aside for dynamic allocation. Unlike the stack, there's no enforced pattern to the allocation and deallocation of blocks from the heap; you can allocate a block at any time and free it at any time. This makes it much more complex to keep track of which parts of the heap are allocated or freed at any given time; there are many custom heap allocators available to tune heap performance for different usage patterns.

Each thread gets a stack, while there's typically only one heap for the application (although it isn't uncommon to have multiple heaps for different types of allocation).

To answer your questions directly:

*To what extent are they controlled by the OS or language runtime?*

The OS allocates the stack for each system-level thread when the thread is created. Typically the OS is called by the language runtime to allocate the heap for the application.

*What is their scope?*

The stack is attached to a thread, so when the thread exits the stack is reclaimed. The heap is typically allocated at application startup by the runtime, and is reclaimed when the application (technically process) exits.

*What determines the size of each of them?*

The size of the stack is set when a thread is created. The size of the heap is set on application startup, but can grow as space is needed (the allocator requests more memory from the operating system).

*What makes one faster?*

The stack is faster because the access pattern makes it trivial to allocate and deallocate memory from it (a pointer/integer is simply incremented or decremented), while the heap has much more complex bookkeeping involved in an allocation or deallocation. Also, each byte in the stack tends to be reused very frequently which means it tends to be mapped to the processor's cache, making it very fast. Another performance hit for the heap is that the heap, being mostly a global resource, typically has to be multi-threading safe, i.e. each allocation and deallocation needs to be - typically - synchronized with "all" other heap accesses in the program.

A clear demonstration:

_{Image source: vikashazrati.wordpress.com}

The bit shifting operators do exactly what their name implies. They shift bits. Here's a brief (or not-so-brief) introduction to the different shift operators.

## The Operators

`>>`

is the arithmetic (or signed) right shift operator.
`>>>`

is the logical (or unsigned) right shift operator.
`<<`

is the left shift operator, and meets the needs of both logical and arithmetic shifts.

All of these operators can be applied to integer values (`int`

, `long`

, possibly `short`

and `byte`

or `char`

). In some languages, applying the shift operators to any datatype smaller than `int`

automatically resizes the operand to be an `int`

.

Note that `<<<`

is not an operator, because it would be redundant.

Also note that **C and C++ do not distinguish between the right shift operators**. They provide only the `>>`

operator, and the right-shifting behavior is implementation defined for signed types. The rest of the answer uses the C# / Java operators.

(In all mainstream C and C++ implementations including GCC and Clang/LLVM, `>>`

on signed types is arithmetic. Some code assumes this, but it isn't something the standard guarantees. It's not *undefined*, though; the standard requires implementations to define it one way or another. However, left shifts of negative signed numbers *is* undefined behaviour (signed integer overflow). So unless you need arithmetic right shift, it's usually a good idea to do your bit-shifting with unsigned types.)

## Left shift (<<)

Integers are stored, in memory, as a series of bits. For example, the number 6 stored as a 32-bit `int`

would be:

```
00000000 00000000 00000000 00000110
```

Shifting this bit pattern to the left one position (`6 << 1`

) would result in the number 12:

```
00000000 00000000 00000000 00001100
```

As you can see, the digits have shifted to the left by one position, and the last digit on the right is filled with a zero. You might also note that shifting left is equivalent to multiplication by powers of 2. So `6 << 1`

is equivalent to `6 * 2`

, and `6 << 3`

is equivalent to `6 * 8`

. A good optimizing compiler will replace multiplications with shifts when possible.

### Non-circular shifting

Please note that these are *not* circular shifts. Shifting this value to the left by one position (`3,758,096,384 << 1`

):

```
11100000 00000000 00000000 00000000
```

results in 3,221,225,472:

```
11000000 00000000 00000000 00000000
```

The digit that gets shifted "off the end" is lost. It does not wrap around.

## Logical right shift (>>>)

A logical right shift is the converse to the left shift. Rather than moving bits to the left, they simply move to the right. For example, shifting the number 12:

```
00000000 00000000 00000000 00001100
```

to the right by one position (`12 >>> 1`

) will get back our original 6:

```
00000000 00000000 00000000 00000110
```

So we see that shifting to the right is equivalent to division by powers of 2.

### Lost bits are gone

However, a shift cannot reclaim "lost" bits. For example, if we shift this pattern:

```
00111000 00000000 00000000 00000110
```

to the left 4 positions (`939,524,102 << 4`

), we get 2,147,483,744:

```
10000000 00000000 00000000 01100000
```

and then shifting back (`(939,524,102 << 4) >>> 4`

) we get 134,217,734:

```
00001000 00000000 00000000 00000110
```

We cannot get back our original value once we have lost bits.

# Arithmetic right shift (>>)

The arithmetic right shift is exactly like the logical right shift, except instead of padding with zero, it pads with the most significant bit. This is because the most significant bit is the *sign* bit, or the bit that distinguishes positive and negative numbers. By padding with the most significant bit, the arithmetic right shift is sign-preserving.

For example, if we interpret this bit pattern as a negative number:

```
10000000 00000000 00000000 01100000
```

we have the number -2,147,483,552. Shifting this to the right 4 positions with the arithmetic shift (-2,147,483,552 >> 4) would give us:

```
11111000 00000000 00000000 00000110
```

or the number -134,217,722.

So we see that we have preserved the sign of our negative numbers by using the arithmetic right shift, rather than the logical right shift. And once again, we see that we are performing division by powers of 2.

## Best Solution

Equal hash means equal file, unless someone malicious is messing around with your files and injecting collisions. (this could be the case if they are downloading stuff from the internet) If that is the case go for a SHA2 based function.

There are no accidental MD5 collisions, 1,47x10

^{-29}is a really really really small number.To overcome the issue of rehashing big files I would have a 3 phased identity scheme.

So if you see a file with a new size you know for certain you do not have a duplicate. And so on.