Skip to content

Commit 1df3694

Browse files
committed
initial commit
0 parents  commit 1df3694

File tree

46 files changed

+4192
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+4192
-0
lines changed

Bitwise Twiddling Tricks.c

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/**
2+
* BIT TWIDDLING TRICKS
3+
* ==============================================
4+
*/
5+
6+
/**
7+
10 - 2
8+
100 - 4
9+
1000 - 8
10+
10000 - 16
11+
100000 - 32
12+
1000000 - 64
13+
10000000 - 128
14+
etc...
15+
**/
16+
17+
18+
/** Extract/Test the first bit from the integer **/
19+
// Example:
20+
int k = 13;
21+
printf("%d", k & 1); // This will extract the 0th bit from the k
22+
// Explaination
23+
/**
24+
* 13 is 1101
25+
* 0001
26+
* ------
27+
* 0001 // This will extract the 0th bit
28+
*/
29+
30+
31+
/** Determine if number is even or odd **/
32+
// Example:
33+
if((n & 1) == 0)
34+
printf("even");
35+
else
36+
printf("odd");
37+
// Explanation
38+
/**
39+
* As before, it will extract the first bit, and by this bit we can know that if number is even or odd
40+
* Because only 0th bit will determine (if it is 1 then odd, else even). Think?
41+
*/
42+
43+
44+
/** Extract/Test the nth bit from the integer **/
45+
// Example:
46+
if(x & (1 << n))
47+
printf("nth-bit is 1");
48+
else
49+
printf("nth-bit is 0");
50+
// Explanation:
51+
/**
52+
* 13 is 1101. And we have to check 2nd bit, so 13 & (1 << 2)
53+
* 1101
54+
* 100
55+
* ------
56+
* 0100
57+
* ------
58+
* So, 2nd bit is 1 or we can say 2nd bit is set
59+
*/
60+
61+
62+
63+
/** Set the nth-bit i.e., make it 1 **/
64+
// Example:
65+
int y = x | (1 << n)
66+
// Explanation:
67+
/**
68+
* 13 is 1101. And we have to set 2nd bit, so 13 | (1 << 2)
69+
* 1101
70+
* 100
71+
* -----
72+
* 1101 // No change, bet 2nd bit already on (or 1)
73+
*
74+
* Now, try for 1st bit, so 13 & (1 << 1)
75+
* 1101
76+
* 10
77+
* ----
78+
* 1111 // Now 1st bit is ON
79+
*/
80+
// If otherwise, we want to set it to zero if it already set to 1. then we XOR(^) operator. eg: 13 ^ (1 << n).
81+
// This is also called toggling of bit (x ^ (1 << n))
82+
83+
84+
85+
86+
/** Unset the nth-bit. i.e., Turns on all the bits except nth-bit **/
87+
// Example:
88+
int y = x & ~(1<<n)
89+
// Explanation:
90+
/**
91+
01111111 (127 in decimal)
92+
& 11101111 (~(1<<4))
93+
--------
94+
01101111
95+
**/
96+
97+
98+
99+
/** Turn off the rightmost (i.e., first 1 bit from the right) 1-bit in a word, producing 0 if none **/
100+
// Formula:
101+
int y = x & (x-1)
102+
/** Example
103+
1011000 (88 in decimal)
104+
& (1011000
105+
- 0000001)
106+
-------
107+
1010000
108+
**/
109+
110+
111+
/** Turn off the rightmost 0-bit in a word **/
112+
// Formula
113+
int y = x | (x + 1)
114+
/** Example
115+
1101
116+
+ 1
117+
----
118+
1110
119+
& 1101
120+
----
121+
1100
122+
----
123+
Adding `1` to any word, transform righmost 1's in a zero and first rightmost 0 in a 1.
124+
**/
125+
126+
127+
128+
/**
129+
* Basic Theorems
130+
*/
131+
132+
// De-morgan's law (first two)
133+
(X & Y & Z ...)` == X` | Y` | Z` ... // Eg: ~(x & y) = ~x | ~y
134+
(X | Y | Z ...)` == X` & Y` & Z` ... // Eg: ~(x | y) = ~x & ~y
135+
136+
137+
~(x + 1) = ~x - 1
138+
~(x - 1) = ~x + 1
139+
~(x^y) = ~x^y
140+
~-x = x - 1
141+
-~x = x + 1
142+
~(x + y) = ~x - y
143+
~(x - y) = ~x + y
144+
-x = ~x + 1 = ~(x - 1)
145+
~x = -x - 1
146+
x + y = x - ~y - 1
147+
= (x ^ y) + 2(x & y)
148+
= (x | y) + (x & y)
149+
= 2(x | y) - (x ^ y)
150+
x - y = x + ~y + 1
151+
= (x ^ y) - 2(~x & y)
152+
= (x & ~y) - (~x & y)
153+
= 2(x & ~y) - (x ^ y)
154+
x^y = (x | y) - (x & y)
155+

Bitwise operators.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Bitwise Operations
2+
// For More: 1) http://graphics.stanford.edu/~seander/bithacks.html
3+
// 2) http://www.catonmat.net/blog/low-level-bit-hacks-you-absolutely-must-know/
4+
5+
<< left shift (equivalent to multiplication by 2)
6+
>> right shift (equivalent to division by 2)
7+
8+
/*
9+
Example:
10+
unsigned short i, j;
11+
i = 13 (binary 0000000000001101)
12+
j = i << 2 (binary 0000000000110100) (equal to 52) (i.e. multiply by 2 two times)
13+
j = i >> 2 (binary 0000000000000011) (equal to 3) (i.e. divide by 2 two times 13/4 == 3)
14+
// Note: Shifting is much faster than actual multiplication or division.
15+
Note: i >>= 2 is same as i = i >> 2,
16+
i <<= 2 is same as i = i << 2
17+
*/
18+
/** WARNING ** -- Beware of shift like this: **/
19+
a << -5
20+
// What does it mean? It is undefined. (on some machine it shift left 27 bits). So avoid this
21+
22+
~ unary operator (bitwise complement)
23+
& bitwise and
24+
^ bitwise exclusive or (XOR)
25+
| bitwise inclusive or (OR) (PIPE)
26+
27+
28+
// `~` operator produces the complement of its operand, with zeros replaced by ones and ones replaced by zeros.
29+
// `&` operator performs boolean `and` operation on all corresponding bits in its two operands.
30+
// `^` and `|` operators are similar (both performs a Boolean `or` operation on the bits in their operands);
31+
// however, `^` produces 0 whenever both operands have a `1` bit, whereas `|` produces 1.
32+
33+
/*
34+
Example:
35+
0 ^ 0 = 0; 0 | 0 = 0;
36+
0 ^ 1 = 1; 0 | 1 = 1;
37+
1 ^ 0 = 1; 1 | 0 = 1;
38+
1 ^ 1 = 0; 1 | 1 = 1;
39+
*/
40+
// Also note that these boolean bitwise operators checks on both operands not on single operands.
41+
// (i.e. in the case of a || b, it checks for one of a or b, but in case of a | b, it checks for both a and b)
42+
43+
44+
/* example */
45+
unsigned short i, j, k;
46+
i = 21; // (binary : 0000000000010101)
47+
j = 56; // (binary : 0000000000111000)
48+
k = ~i; // k is now 65514 (binary : 1111111111101010)
49+
k = i & j; // k is now 16 (binary : 0000000000010000)
50+
k = i ^ j; // k is now 45 (binary : 0000000000101101)
51+
k = i | j; // k is now 61 (binary : 0000000000111101)
52+
53+
Precedence
54+
Highest ~
55+
&
56+
^
57+
Lowest |
58+
59+
60+
/**
61+
* -------------------- Important tricks ----------------------
62+
*/
63+
64+
/**
65+
* Sometimes, you see the constant define as
66+
* 0100, 0200, 0400 -- these are 64, 128, 256 in decimal (by converting from octal to decimal)
67+
*/

C Headers.c

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
2+
/* C Common Headers */
3+
4+
#include <assert.h> // void assert(scalar expression)
5+
#include <errno.h> // C errors (defines macro -- errno, EDOM, ERANGE, ELLSEQ)
6+
#include <ctype.h>
7+
#include <float.h>
8+
#include <iso646.h> // Defines (macro:-- and, and_eq, etc... (equivalent to operators))
9+
#include <locale.h>
10+
#include <limits.h> // define macros like INT_MAX, INT_MIN, LONG_MAX, etc..
11+
#include <complex.h>
12+
#include <math.h>
13+
#include <stdarg.h>
14+
#include <setjmp.h>
15+
#include <signal.h>
16+
#include <stdbool.h>
17+
#include <string.h>
18+
#include <stdlib.h> // Algorithms: qsort, bsearch
19+
#include <stdio.h> // Types: size_t, NULL, EOF
20+
#include <time.h>
21+
#include <stddef.h> // Types and macros: ptrdiff_t, size_t, max_align_t, offsetof
22+
#include <stdint.h> // C99 (defines types like intmax_t, int8_t, intptr_t, int_fast32_t etc...)
23+
// here is a good tutorial on <inttypes.h>
24+
// http://h21007.www2.hp.com/portal/site/dspp/menuitem.863c3e4cbcdc3f3515b49c108973a801/?jumpid=reg_R1002_USEN&ciid=a868e67018695110VgnVCM100000275d6e10RCRD#AUTO3
25+
#include <inttypes.h> // C99 - It also include <stdint.h>
26+
#include <wchar.h> // C99
27+
#include <wctype.h> // C99
28+
#include <tgmath.h> // C99
29+
#include <fenv.h> // C99
30+
#include <uchar.h> // C11 (for Unicode character support)
31+
#include <stdaligh.h> // C11
32+
#include <stdatomic.h> // C11
33+
#
34+
35+
// Preprocessors
36+
37+
#if
38+
#ifdef
39+
#ifndef
40+
#else
41+
#elif
42+
#endif
43+
#include
44+
#define
45+
#undef
46+
#warning
47+
#error
48+
#line
49+
50+
51+
// To continue a directive to the next line, we must end the current line with a \ character. For eg:
52+
53+
/*
54+
#define DISK_CAPACITY (SIDES * \
55+
TRACK_PER_SEC* \
56+
SECTOR_PER_TRACK* \
57+
BYTES_PER_SECTOR)
58+
59+
*/
60+
61+
62+
// Predefined Macros - Search google for this
63+
64+
__LINE__ // Line number of file being compiled
65+
__FILE__ // Name of file being compiled
66+
__DATE__ // Date of compilation (in the form "Mmm dd yyyy")
67+
__TIME__ // Time of compilation (in the form "hh: mm: ss")
68+
__STDC__ // 1 if the compiler conforms to the C standard (C89 or C99 or C11)
69+
__cpluscplus // This macro test whether the C++ compiler is in use - GCC macro
70+
__func__
71+
__ASSEMBLER__
72+
73+
// Additional C99 Macros
74+
75+
__STDC__HOSTED__ // 1 if this is a hosted implementation (i.e. we use API of UNIX, Microsoft); 0 if it is freestanding
76+
__STDC__VERSION__ // Version of C standard support
77+
78+
// Others are __STDC_IEC_559__ , __STDC_IEC_559_COMPLEX__ , __STDC_ISO_10646__
79+
80+
81+
82+
83+
84+
// Include files other than `.h` extension. The compiler doesn't care about the extension of the file you're including,
85+
// so things like ".h", ".hpp", ".hxx", ".hh", ".inl", ".inc", etc. are a simple convention, to make it clear what the files is supposed to contain.
86+
// So, you can define whatever in it. Example:
87+
// Suppose you define a function in `.inc` file
88+
/*
89+
int add(a, b) {
90+
return a+b;
91+
}
92+
93+
So, it works if you compile it with main file. This means compiler doesn't care about the type of extension it can be whatever. But standard suggest
94+
that you can define function prototypes in a `.h` extension file.
95+
96+
Some people use of `.inc` or `.inl` file extension to define inline function or some characters constant. It can be useful too.
97+
Also, file with no extension also work.
98+
In linux implementation, you'll see this type of file `.inc` or `.inl`
99+
*/
100+
See this: http://stackoverflow.com/questions/1208028/significance-of-a-inl-file-in-c

Common math.h functions.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Important Math functions - for full see reference
2+
3+
// absolute value
4+
double fabs(double x);
5+
double abs(double x), long double abs(long double x), float abs(float x); // Note that, all the below function have these three types
6+
7+
// Trigonometric functions
8+
double sin(double arg); // where arg in radians
9+
double cos(double arg);
10+
double tan(double arg);
11+
double asin(double arg); // arcsin i.e. inverse of sin
12+
double acos(double arg);
13+
double atan(double arg);
14+
double atan2(double y, double x); // C99
15+
// Similar for hyperbolic functions: sinh, cosh, tanh, asinh, acosh, atanh
16+
17+
18+
// Power functions
19+
double sqrt(double arg);
20+
double pow(double base, double exp);
21+
double hypot(double x, double y); // C99 - hypotenuse function
22+
23+
24+
// Exponential functions
25+
double exp(double arg); // return e[euler, 2.7182818] raised to the given power arg
26+
double log(double arg); // natural logarithm base e
27+
double log10(double arg); // base 10 logarithm
28+
double log2(double arg); // base 2 logarithm, C99
29+
double exp2(double arg); // return 2 raised to the given power(2 power x);
30+
31+
32+
// Nearest integer floating point operations
33+
double ceil(double arg);
34+
double floor(double arg);
35+
36+
// Max and min functions - C99
37+
double fdim(double x, double y); // Positive difference between x and y
38+
double fmax(double x, double y);
39+
double fmin(double x, double y);
40+
41+
// Basic operations (macros)
42+
isfinite(x) // returns whether x is finite
43+
isinf(x) // returns whether x if infinity
44+
isnan(x) // returns whether x is not-a-number (not-a-number is 0/0 or negative square root)
45+
signbit(x)
46+
isnormal(x)
47+
48+
49+
// Other Integer arithmetics - From <stdlib.h>
50+
int abs(int n);
51+
long int abs(long int n);
52+
long long int llabs(long long int n); // From C99
53+
54+
div_t div(int n, int denom);
55+
ldiv_t div(long int n, long int denom);
56+
lldiv_t div(long long int n, long long int denom);
57+
58+
59+
60+
// Macros
61+
INFINITY
62+
NAN

0 commit comments

Comments
 (0)