Hello 0
Hello 1
Hello 2
Hello 3
Hello 4
Hello 6
Hello 7
Hello 8
Hello 9
Hello 10
C - Input and Output
Input : In any programming language input means to feed some data into program. This can be given in the form of file or from command line. C programming language provides a set of built-in functions to read given input and feed it to the program as per requirement.
Output : In any programming language output means to display some data on screen, printer or in any file. C programming language provides a set of built-in functions to output required data.
Here we will discuss only one input function and one putput function just to understand the meaning of input and output. Rest of the functions are given into C - Built-in Functions
printf() function
This is one of the most frequently used functions in C for output. ( we will discuss what is function in subsequent chapter. ).
Try following program to understand printf() function.
#include <stdio.h>
main()
{
int dec = 5;
char str[] = "abc";
char ch = 's';
float pi = 3.14;
printf("%d %s %f %c\n", dec, str, pi, ch);
}
|
The output of the above would be:
Here %d is being used to print an integer, %s is being usedto print a string, %f is being used to print a float and %c is being used to print a character.
A complete syntax of printf() function is given in C - Built-in Functions
scanf() function
This is the function which can be used to to read an input from the command line.
Try following program to understand scanf() function.
#include <stdio.h>
main()
{
int x;
int args;
printf("Enter an integer: ");
if (( args = scanf("%d", &x)) == 0) {
printf("Error: not an integer\n");
} else {
printf("Read in %d\n", x);
}
}
|
Here %d is being used to read an integer value and we are passing &x to store the vale read input. Here &indicates the address of variavle x.
This program will prompt you to enter a value. Whatever value you will enter at command prompt that will be output at the screen using printf() function. If you eneter a non-integer value then it will display an error message.
Enter an integer: 20
Read in 20
|
A complete set of input output functions is given in C - Built-in Functions
C - Pointing to Data
A pointer is a special kind of variable. Pointers are designed for storing memory address i.e. the address of another variable. Declaring a pointer is the same as declaring a normal variable except you stick an asterisk '*' in front of the variables identifier.
- There are two new operators you will need to know to work with pointers. The "address of" operator '&' and the "dereferencing" operator '*'. Both are prefix unary operators.
- When you place an ampersand in front of a variable you will get it's address, this can be stored in a pointer vairable.
- When you place an asterisk in front of a pointer you will get the value at the memory address pointed to.
Here is an example to understand what I have stated above.
#include <stdio.h>
int main()
{
int my_variable = 6, other_variable = 10;
int *my_pointer;
printf("the address of my_variable is : %p\n", &my_variable);
printf("the address of other_variable is : %p\n", &other_variable);
my_pointer = &my_variable;
printf("\nafter \"my_pointer = &my_variable\":\n");
printf("\tthe value of my_pointer is %p\n", my_pointer);
printf("\tthe value at that address is %d\n", *my_pointer);
my_pointer = &other_variable;
printf("\nafter \"my_pointer = &other_variable\":\n");
printf("\tthe value of my_pointer is %p\n", my_pointer);
printf("\tthe value at that address is %d\n", *my_pointer);
return 0;
}
|
This will produce following result.
the address of my_variable is : 0xbfffdac4
the address of other_variable is : 0xbfffdac0
after "my_pointer = &my_variable":
the value of my_pointer is 0xbfffdac4
the value at that address is 6
after "my_pointer = &other_variable":
the value of my_pointer is 0xbfffdac0
the value at that address is 10
|
Pointers and Arrays
The most frequent use of pointers in C is for walking efficiently along arrays. In fact, in the implementation of an array, the array name represents the address of the zeroth element of the array, so you can't use it on the left side of an expression. For example:
y is of type pointer to character (although it doesn't yet point anywhere). We can make y point to an element of x by either of
Since x is the address of x[0] this is legal and consistent. Now `*y' gives x[0]. More importantly notice the following:
*(y+1) gives x[1]
*(y+i) gives x[i]
and the sequence
y = &x[0];
y++;
leaves y pointing at x[1].
|
Pointer Arithmetic:
C is one of the few languages that allows pointer arithmetic. In other words, you actually move the pointer reference by an arithmetic operation. For example:
int x = 5, *ip = &x;
ip++;
|
On a typical 32-bit machine, *ip would be pointing to 5 after initialization. But ip++; increments the pointer 32-bits or 4-bytes. So whatever was in the next 4-bytes, *ip would be pointing at it.
Pointer arithmetic is very useful when dealing with arrays, because arrays and pointers share a special relationship in C.
Using Pointer Arithmetic With Arrays:
Arrays occupy consecutive memory slots in the computer's memory. This is where pointer arithmetic comes in handy - if you create a pointer to the first element, incrementing it one step will make it point to the next element.
#include <stdio.h>
int main() {
int *ptr;
int arrayInts[10] = {1,2,3,4,5,6,7,8,9,10};
ptr = arrayInts; /* ptr = &arrayInts[0]; is also fine */
printf("The pointer is pointing to the first ");
printf("array element, which is %d.\n", *ptr);
printf("Let's increment it.....\n");
ptr++;
printf("Now it should point to the next element,");
printf(" which is %d.\n", *ptr);
printf("But suppose we point to the 3rd and 4th: %d %d.\n",
*(ptr+1),*(ptr+2));
ptr+=2;
printf("Now skip the next 4 to point to the 8th: %d.\n",
*(ptr+=4));
ptr--;
printf("Did I miss out my lucky number %d?!\n", *(ptr++));
printf("Back to the 8th it is then..... %d.\n", *ptr);
return 0;
}
|
This will produce following result:
The pointer is pointing to the first array element, which is 1.
Let's increment it.....
Now it should point to the next element, which is 2.
But suppose we point to the 3rd and 4th: 3 4.
Now skip the next 4 to point to the 8th: 8.
Did I miss out my lucky number 7?!
Back to the 8th it is then..... 8.
|
See more examples on Pointers and Array
Pointers and const Type Qualifier:
- The const type qualifier can make things a little confusing when it is used with pointer declarations.
- The below example:
const int * const ip; /* The pointer *ip is const
and it points at is also cont */
int * const ip; /* The pointer *ip is const */
const int * ip; /* What *ip is pointing at is const */
int * ip; /* Nothing is const */
|
As you can see, you must be careful when specifying the const qualifier when using pointers.
Modifying Variables Using Pointers:
You know how to access the value pointed to using the dereference operator, but you can also modify the content of variables. To achieve this, put the dereferenced pointer on the left of the assignment operator, as shown in this example, which uses an array:
#include <stdio.h>
int main() {
char *ptr;
char arrayChars[8] = {'F','r','i','e','n','d','s','\0'};
ptr = arrayChars;
printf("The array reads %s.\n", arrayChars);
printf("Let's change it..... ");
*ptr = 'f'; /* ptr points to the first element */
printf(" now it reads %s.\n", arrayChars);
printf("The 3rd character of the array is %c.\n",
*(ptr+=2));
printf("Let's change it again..... ");
*(ptr - 1) = ' ';
printf("Now it reads %s.\n", arrayChars);
return 0;
}
|
This will produce following result:
The array reads Friends.
Let's change it..... now it reads friends.
The 3rd character of the array is i.
Let's change it again..... Now it reads f iends.
|
Generic Pointers: ( void Pointer )
When a variable is declared as being a pointer to type void it is known as a generic pointer. Since you cannot have a variable of type void, the pointer will not point to any data and therefore cannot be dereferenced. It is still a pointer though, to use it you just have to cast it to another kind of pointer first. Hence the term Generic pointer. This is very useful when you want a pointer to point to data of different types at different times.
Try the following code to understand Generic Pointers.
#include <stdio.h>
int main()
{
int i;
char c;
void *the_data;
i = 6;
c = 'a';
the_data = &i;
printf("the_data points to the integer value %d\n",
*(int*) the_data);
the_data = &c;
printf("the_data now points to the character %c\n",
*(char*) the_data);
return 0;
}
|
NOTE-1 : Here in first print statement, the_data is prefixed by *(int*). This is called type casting in C language.Type is used to caste a variable from one data type to another datatype to make it compatible to the lvalue.
NOTE-2 : lvalue is something which is used to left side of a statement and in which we can assign some value. A constant can't be an lvalue because we can not assign any value in contact. For example x = y, here x is lvalue and y is rvalue.
However, above example will produce following result:
the_data points to the integer value 6
the_data now points to the character a
C - Using Functions
A function is a module or block of program code which deals with a particular task. Making functions is a way of isolating one block of code from other independent blocks of code.
Functions serve two purposes.
- They allow a programmer to say: `this piece of code does a specific job which stands by itself and should not be mixed up with anyting else',
- Second they make a block of code reusable since a function can be reused in many different contexts without repeating parts of the program text.
A function can take a number of parameters, do required processing and then return a value. There may be a function which does not return any value.
You already have seen couple of built-in functions like printf(); Similar way you can define your own functions in C language.
Consider the following chunk of code
int total = 10;
printf("Hello World");
total = total + l;
|
To turn it into a function you simply wrap the code in a pair of curly brackets to convert it into a single compound statement and write the name that you want to give it in front of the brackets:
Demo()
{
int total = 10;
printf("Hello World");
total = total + l;
}
|
curved brackets after the function's name are required. You can pass one or more paramenters to a function as follows:
Demo( int par1, int par2)
{
int total = 10;
printf("Hello World");
total = total + l;
}
|
By default function does not return anything. But you can make a function to return any value as follows:
int Demo( int par1, int par2)
{
int total = 10;
printf("Hello World");
total = total + l;
return total;
}
|
A return keyword is used to return a value and datatype of the returned value is specified before the name of function. In this case function returns total which is int type. If a function does not return a value then void keyword can be used as return value.
Once you have defined your function you can use it within a program:
Functions and Variables:
Each function behaves the same way as C language standard function main(). So a function will have its own local variables defined. In the above example total variable is local to the function Demo.
A global variable can be accessed in any function in similar way it is accessed in main() function.
Declaration and Definition
When a function is defined at any place in the program then it is called function definition. At the time of definition of a function actual logic is implemented with-in the function.
A function declaration does not have any body and they just have their interfaces.
A function declaration is usually declared at the top of a C source file, or in a separate header file.
A function declaration is sometime called function prototype or function signature. For the above Demo() function which returns an integer, and takes two parameters a function declaration will be as follows:
int Demo( int par1, int par2);
|
Passing Parameters to a Function
There are two ways to pass parameters to a function:
- Pass by Value: mechanism is used when you don't want to change the value of passed paramters. When parameters are passed by value then functions in C create copies of the passed in variables and do required processing on these copied variables.
- Pass by Reference mechanism is used when you want a function to do the changes in passed parameters and reflect those changes back to the calling function. In this case only addresses of the variables are passed to a function so that function can work directly over the addresses.
Here are two programs to understand the difference: First example is for Pass by value:
#include <stdio.h>
/* function declaration goes here.*/
void swap( int p1, int p2 );
int main()
{
int a = 10;
int b = 20;
printf("Before: Value of a = %d and value of b = %d\n", a, b );
swap( a, b );
printf("After: Value of a = %d and value of b = %d\n", a, b );
}
void swap( int p1, int p2 )
{
int t;
t = p2;
p2 = p1;
p1 = t;
printf("Value of a (p1) = %d and value of b(p2) = %d\n", p1, p2 );
}
|
Here is the result produced by the above example. Here the values of a and b remain unchanged before calling swap function and after calling swap function.
Before: Value of a = 10 and value of b = 20
Value of a (p1) = 20 and value of b(p2) = 10
After: Value of a = 10 and value of b = 20
|
Following is the example which demonstrate the concept of pass by reference
#include <stdio.h>
/* function declaration goes here.*/
void swap( int *p1, int *p2 );
int main()
{
int a = 10;
int b = 20;
printf("Before: Value of a = %d and value of b = %d\n", a, b );
swap( &a, &b );
printf("After: Value of a = %d and value of b = %d\n", a, b );
}
void swap( int *p1, int *p2 )
{
int t;
t = *p2;
*p2 = *p1;
*p1 = t;
printf("Value of a (p1) = %d and value of b(p2) = %d\n", *p1, *p2 );
}
|
Here is the result produced by the above example. Here the values of a and b are changes after calling swap function.
Before: Value of a = 10 and value of b = 20
Value of a (p1) = 20 and value of b(p2) = 10
After: Value of a = 20 and value of b = 10
C - Play with Strings
- A structure in C is a collection of items of different types. You can think of a structure as a "record" is in Pascal or a class in Java without methods.
- Structures, or structs, are very useful in creating data structures larger and more complex than the ones we have discussed so far.
- Simply you can group various built-in data types into a structure.
- Object conepts was derived from Structure concept. You can achieve few object oriented goals using C structure but it is very complex.
Following is the example how to define a structure.
struct student {
char firstName[20];
char lastName[20];
char SSN[9];
float gpa;
};
|
Now you have a new datatype called student and you can use this datatype define your variables of student type:
struct student student_a, student_b;
or an array of students as
struct student students[50];
|
Another way to declare the same thing is:
struct {
char firstName[20];
char lastName[20];
char SSN[10];
float gpa;
} student_a, student_b;
|
All the variables inside an structure will be accessed using these values as student_a.firstName will give value of firstName variable. Similarly we can aqccess other variables.
Structure Example:
Try out following example to understand the concept:
#include <stdio.h>
struct student {
char firstName[20];
char lastName[20];
char SSN[10];
float gpa;
};
main()
{
struct student student_a;
strcpy(student_a.firstName, "Deo");
strcpy(student_a.lastName, "Dum");
strcpy(student_a.SSN, "2333234" );
student_a.gpa = 2009.20;
printf( "First Name: %s\n", student_a.firstName );
printf( "Last Name: %s\n", student_a.lastName );
printf( "SNN : %s\n", student_a.SSN );
printf( "GPA : %f\n", student_a.gpa );
}
|
This will produce following results:
First Name: Deo
Last Name: Dum
SSN : 2333234
GPA : 2009.20
|
Pointers to Structs:
Sometimes it is useful to assign pointers to structures (this will be evident in the next section with self-referential structures). Declaring pointers to structures is basically the same as declaring a normal pointer:
struct student *student_a;
|
To dereference, you can use the infix operator: ->.
printf("%s\n", student_a->SSN);
|
typedef Keyword
There is an easier way to define structs or you could "alias" types you create. For example:
typedef struct{
char firstName[20];
char lastName[20];
char SSN[10];
float gpa;
}student;
|
Now you can use student directly to define variables of student type without using struct keyword. Following is the example:
You can use typedef for non-structs:
typedef long int *pint32;
pint32 x, y, z;
|
x, y and z are all pointers to long ints
Unions Datatype
Unions are declared in the same fashion as structs, but have a fundamental difference. Only one item within the union can be used at any time, because the memory allocated for each item inside the union is in a shared memory location.
Here is how we define a Union
union Shape {
int circle;
int triangle;
int ovel;
};
|
We use union in such case where only one condition will be applied and only one variable will be used.
Conclusion:
- You can create arrays of structs.
- Structs can be copied or assigned.
- The & operator may be used with structs to show addresses.
- Structs can be passed into functions. Structs can also be returned from functions.
- Structs cannot be compared!
- Structures can store non-homogenous data types into a single collection, much like an array does for common data (except it isn't accessed in the same manner).
- Pointers to structs have a special infix operator: -> for dereferencing the pointer.
- typedef can help you clear your code up and can help save some keystrokes.
|
- In C language Strings are defined as an array of characters or a pointer to a portion of memory containing ASCII characters. A string in C is a sequence of zero or more characters followed by a NULL '\0' character:
- It is important to preserve the NULL terminating character as it is how C defines and manages variable length strings. All the C standard library functions require this for successful operation.
- All the string handling functions are prototyped in: string.h or stdio.h standard header file. So while using any string related function, don't forget to include either stdio.h or string.h. May be your compiler differes so please check before going ahead.
- If you were to have an array of characters WITHOUT the null character as the last element, you'd have an ordinary character array, rather than a string constant.
- String constants have double quote marks around them, and can be assigned to char pointers as shown below. Alternatively, you can assign a string constant to a char array - either with no size specified, or you can specify a size, but don't forget to leave a space for the null character!
char *string_1 = "Hello";
char string_2[] = "Hello";
char string_3[6] = "Hello";
|
Reading and Writing Strings:
One possible way to read in a string is by using scanf. However, the problem with this, is that if you were to enter a string which contains one or more spaces, scanf would finish reading when it reaches a space, or if return is pressed. As a result, the string would get cut off. So we could use the gets function
A gets takes just one argument - a char pointer, or the name of a char array, but don't forget to declare the array / pointer variable first! What's more, is that it automatically prints out a newline character, making the output a little neater.
A puts function is similar to gets function in the way that it takes one argument - a char pointer. This also automatically adds a newline character after printing out the string. Sometimes this can be a disadvantage, so printf could be used instead.
#include <stdio.h>
int main() {
char array1[50];
char *array2;
printf("Now enter another string less than 50");
printf(" characters with spaces: \n");
gets(array1);
printf("\nYou entered: ");
puts(array1);
printf("\nTry entering a string less than 50");
printf(" characters, with spaces: \n");
scanf("%s", array2);
printf("\nYou entered: %s\n", array2);
return 0;
}
|
This will produce following result:
Now enter another string less than 50 characters with spaces:
hello world
You entered: hello world
Try entering a string less than 50 characters, with spaces:
hello world
You entered: hello
|
String Manipulation Functions
- char *strcpy (char *dest, char *src) - Copy src string string into dest string.
- char *strncpy(char *string1, char *string2, int n) - Copy first n characters of string2 to stringl .
- int strcmp(char *string1, char *string2) - Compare string1 and string2 to determine alphabetic order.
- int strncmp(char *string1, char *string2, int n) - Compare first n characters of two strings.
- int strlen(char *string) - Determine the length of a string.
- char *strcat(char *dest, const char *src); - Concatenate string src to the string dest.
- char *strncat(char *dest, const char *src, int n); - Concatenate n chracters from string src to the string dest.
- char *strchr(char *string, int c) - Find first occurrence of character c in string.
- char *strrchr(char *string, int c) - Find last occurrence of character c in string.
- char *strstr(char *string2, char string*1) - Find first occurrence of string string1 in string2.
- char *strtok(char *s, const char *delim) - Parse the string s into tokens using delim as delimiter.
C - Structured Datatypes
- A structure in C is a collection of items of different types. You can think of a structure as a "record" is in Pascal or a class in Java without methods.
- Structures, or structs, are very useful in creating data structures larger and more complex than the ones we have discussed so far.
- Simply you can group various built-in data types into a structure.
- Object conepts was derived from Structure concept. You can achieve few object oriented goals using C structure but it is very complex.
Following is the example how to define a structure.
struct student {
char firstName[20];
char lastName[20];
char SSN[9];
float gpa;
};
|
Now you have a new datatype called student and you can use this datatype define your variables of student type:
struct student student_a, student_b;
or an array of students as
struct student students[50];
|
Another way to declare the same thing is:
struct {
char firstName[20];
char lastName[20];
char SSN[10];
float gpa;
} student_a, student_b;
|
All the variables inside an structure will be accessed using these values as student_a.firstName will give value of firstName variable. Similarly we can aqccess other variables.
Structure Example:
Try out following example to understand the concept:
#include <stdio.h>
struct student {
char firstName[20];
char lastName[20];
char SSN[10];
float gpa;
};
main()
{
struct student student_a;
strcpy(student_a.firstName, "Deo");
strcpy(student_a.lastName, "Dum");
strcpy(student_a.SSN, "2333234" );
student_a.gpa = 2009.20;
printf( "First Name: %s\n", student_a.firstName );
printf( "Last Name: %s\n", student_a.lastName );
printf( "SNN : %s\n", student_a.SSN );
printf( "GPA : %f\n", student_a.gpa );
}
|
This will produce following results:
First Name: Deo
Last Name: Dum
SSN : 2333234
GPA : 2009.20
|
Pointers to Structs:
Sometimes it is useful to assign pointers to structures (this will be evident in the next section with self-referential structures). Declaring pointers to structures is basically the same as declaring a normal pointer:
struct student *student_a;
|
To dereference, you can use the infix operator: ->.
printf("%s\n", student_a->SSN);
|
typedef Keyword
There is an easier way to define structs or you could "alias" types you create. For example:
typedef struct{
char firstName[20];
char lastName[20];
char SSN[10];
float gpa;
}student;
|
Now you can use student directly to define variables of student type without using struct keyword. Following is the example:
You can use typedef for non-structs:
typedef long int *pint32;
pint32 x, y, z;
|
x, y and z are all pointers to long ints
Unions Datatype
Unions are declared in the same fashion as structs, but have a fundamental difference. Only one item within the union can be used at any time, because the memory allocated for each item inside the union is in a shared memory location.
Here is how we define a Union
union Shape {
int circle;
int triangle;
int ovel;
};
|
We use union in such case where only one condition will be applied and only one variable will be used.
Conclusion:
- You can create arrays of structs.
- Structs can be copied or assigned.
- The & operator may be used with structs to show addresses.
- Structs can be passed into functions. Structs can also be returned from functions.
- Structs cannot be compared!
- Structures can store non-homogenous data types into a single collection, much like an array does for common data (except it isn't accessed in the same manner).
- Pointers to structs have a special infix operator: -> for dereferencing the pointer.
- typedef can help you clear your code up and can help save some keystrokes.
|
C - Working with Files
When accessing files through C, the first necessity is to have a way to access the files. For C File I/O you need to use a FILE pointer, which will let the program keep track of the file being accessed. For Example:
To open a file you need to use the fopen function, which returns a FILE pointer. Once you've opened a file, you can use the FILE pointer to let the compiler perform input and output functions on the file.
FILE *fopen(const char *filename, const char *mode);
|
Here filename is string literal which you will use to name your file and mode can have one of the following values
w - open for writing (file need not exist)
a - open for appending (file need not exist)
r+ - open for reading and writing, start at beginning
w+ - open for reading and writing (overwrite file)
a+ - open for reading and writing (append if file exists)
|
Note that it's possible for fopen to fail even if your program is perfectly correct: you might try to open a file specified by the user, and that file might not exist (or it might be write-protected). In those cases, fopen will create a file if you specify file mode as "w", "w+", "a", or "a+" otherwise it will return 0, the NULL pointer.
Here's a simple example of using fopen:
FILE *fp;
fp=fopen("/home/tutorialspoint/test.txt", "r");
|
This code will open test.txt for reading in text mode. To open a file in a binary mode you must add a b to the end of the mode string; for example, "rb" (for the reading and writing modes, you can add the b either after the plus sign - "r+b" - or before - "rb+")
To close a function you can use the function:
int fclose(FILE *a_file);
|
fclose returns zero if the file is closed successfully.
An example of fclose is:
To work with text input and output, you use fprintf and fscanf, both of which are similar to their friends printf and scanf except that you must pass the FILE pointer as first argument.
Try out following example:
#include <stdio.h>
main()
{
FILE *fp;
fp = fopen("/tmp/test.txt", "w");
fprintf(fp, "This is testing...\n");
fclose(fp;);
}
|
Thsi will create a file test.txt in /tmp directory and will write This is testing in that file.
Here is an example which will be used to read lines from a file:
#include <stdio.h>
main()
{
FILE *fp;
char buffer[20];
fp = fopen("/tmp/test.txt", "r");
fscanf(fp, "%s", buffer);
printf("Read Buffer: %s\n", %buffer );
fclose(fp;);
}
|
It is also possible to read (or write) a single character at a time--this can be useful if you wish to perform character-by-character input. The fgetc function, which takes a file pointer, and returns an int, will let you read a single character from a file:
The fgetc returns an int. What this actually means is that when it reads a normal character in the file, it will return a value suitable for storing in an unsigned char (basically, a number in the range 0 to 255). On the other hand, when you're at the very end of the file, you can't get a character value--in this case, fgetc will return "EOF", which is a constnat that indicates that you've reached the end of the file.
The fputc function allows you to write a character at a time--you might find this useful if you wanted to copy a file character by character. It looks like this:
int fputc( int c, FILE *fp );
|
Note that the first argument should be in the range of an unsigned char so that it is a valid character. The second argument is the file to write to. On success, fputc will return the value c, and on failure, it will return EOF.
Binary I/O
There are following two functions which will be used for binary input and output:
size_t fread(void *ptr, size_t size_of_elements,
size_t number_of_elements, FILE *a_file);
size_t fwrite(const void *ptr, size_t size_of_elements,
size_t number_of_elements, FILE *a_file);
|
Both of these functions deal with blocks of memories - usually arrays. Because they accept pointers, you can also use these functions with other data structures; you can even write structs to a file or a read struct into memory.
C - Bits Manipulation
Bit manipulation is the act of algorithmically manipulating bits or other pieces of data shorter than a byte. C language is very efficient in manipulating bits.
Here are following operators to perform bits manipulation:
Bitwise Operators:
Bitwise operator works on bits and perform bit by bit operation.
Assume if B = 60; and B = 13; Now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1000
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
Show Examples
There are following Bitwise operators supported by C language
Operator | Description | Example |
& | Binary AND Operator copies a bit to the result if it exists in both operands. | (A & B) will give 12 which is 0000 1100 |
| | Binary OR Operator copies a bit if it exists in eather operand. | (A | B) will give 61 which is 0011 1101 |
^ | Binary XOR Operator copies the bit if it is set in one operand but not both. | (A ^ B) will give 49 which is 0011 0001 |
~ | Binary Ones Complement Operator is unary and has the efect of 'flipping' bits. | (~A ) will give -60 which is 1100 0011 |
<< | Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. | A << 2 will give 240 which is 1111 0000 |
>> | Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. | A >> 2 will give 15 which is 0000 1111 |
The shift operators perform appropriate shift by operator on the right to the operator on the left. The right operator must be positive. The vacated bits are filled with zero.
For example: x << 2 shifts the bits in x by 2 places to the left.
if x = 00000010 (binary) or 2 (decimal)
then:
x >>= 2 => x = 00000000 or just 0 (decimal)
Also: if x = 00000010 (binary) or 2 (decimal)
then
x <<= 2 => x = 00001000 or 8 (decimal)
|
Therefore a shift left is equivalent to a multiplication by 2. Similarly a shift right is equal to division by 2. Shifting is much faster than actual multiplication (*) or division (/) by 2. So if you want fast multiplications or division by 2 use shifts.
To illustrate many points of bitwise operators let us write a function, Bitcount, that counts bits set to 1 in an 8 bit number (unsigned char) passed as an argument to the function.
int bitcount(unsigned char x)
{
int count;
for ( count=0; x != 0; x>>=1);
{
if ( x & 01)
count++;
}
return count;
}
|
This function illustrates many C program points:
- for loop not used for simple counting operation.
- x >>= 1 => x = x>> 1;
- for loop will repeatedly shift right x until x becomes 0
- use expression evaluation of x & 01 to control if
- x & 01 masks of 1st bit of x if this is 1 then count++
Bit Fields
Bit Fields allow the packing of data in a structure. This is especially useful when memory or data storage is at a premium. Typical examples:
- Packing several objects into a machine word. e.g. 1 bit flags can be compacted.
- Reading external file formats -- non-standard file formats could be read in. E.g. 9 bit integers.
C allows us do this in a structure definition by putting :bit length after the variable.For example:
struct packed_struct {
unsigned int f1:1;
unsigned int f2:1;
unsigned int f3:1;
unsigned int f4:1;
unsigned int type:4;
unsigned int my_int:9;
} pack;
|
Here the packed_struct contains 6 members: Four 1 bit flags f1..f3, a 4 bit type and a 9 bit my_int.
C automatically packs the above bit fields as compactly as possible, provided that the maximum length of the field is less than or equal to the integer word length of the computer. If this is not the case then some compilers may allow memory overlap for the fields whilst other would store the next field in the next word.
C - Pre-Processors
The C Preprocessor is not part of the compiler, but is a separate step in the compilation process. In simplistic terms, a C Preprocessor is just a text substitution tool. We'll refer to the C Preprocessor as the CPP.
All preprocessor lines begin with #
- The unconditional directives are:
- #include - Inserts a particular header from another file
- #define - Defines a preprocessor macro
- #undef - Undefines a preprocessor macro
- The conditional directives are:
- #ifdef - If this macro is defined
- #ifndef - If this macro is not defined
- #if - Test if a compile time condition is true
- #else - The alternative for #if
- #elif - #else an #if in one statement
- #endif - End preprocessor conditional
- Other directives include:
- # - Stringization, replaces a macro parameter with a string constant
- ## - Token merge, creates a single token from two adjacent ones
Pre-Processors Examples:
Analyze following examples to understand various directives.
#define MAX_ARRAY_LENGTH 20
|
Tells the CPP to replace instances of MAX_ARRAY_LENGTH with 20. Use #define for constants to increase readability.
#include <stdio.h>
#include "myheader.h"
|
Tells the CPP to get stdio.h from System Libraries and add the text to this file. The next line tells CPP to get myheader.h from the local directory and add the text to the file.
#undef FILE_SIZE
#define FILE_SIZE 42
|
Tells the CPP to undefine FILE_SIZE and define it for 42.
#ifndef MESSAGE
#define MESSAGE "You wish!"
#endif
|
Tells the CPP to define MESSAGE only if MESSAGE isn't defined already.
#ifdef DEBUG
/* Your debugging statements here */
#endif
|
Tells the CPP to do the following statements if DEBUG is defined. This is useful if you pass the -DDEBUG flag to gcc. This will define DEBUG, so you can turn debugging on and off on the fly!
Stringize (#):
The stringize or number-sign operator ('#'), when used within a macro definition, converts a macro parameter into a string constant. This operator may be used only in a macro that has a specified argument or parameter list.
When the stringize operator immediately precedes the name of one of the macro parameters, the parameter passed to the macro is enclosed within quotation marks and is treated as a string literal. For example:
#include <stdio.h>
#define message_for(a, b) \
printf(#a " and " #b ": We love you!\n")
int main(void)
{
message_for(Carole, Debra);
return 0;
}
|
This will produce following result using stringization macro message_for
Carole and Debra: We love you!
|
Token Pasting (##):
The token-pasting operator (##) within a macro definition combines two arguments. It permits two separate tokens in the macro definition to be joined into a single token.
If the name of a macro parameter used in the macro definition is immediately preceded or followed by the token-pasting operator, the macro parameter and the token-pasting operator are replaced by the value of the passed parameter.Text that is adjacent to the token-pasting operator that is not the name of a macro parameter is not affected. For example:
#define tokenpaster(n) printf ("token" #n " = %d", token##n)
tokenpaster(34);
|
This example results in the following actual output from the preprocessor:
printf ("token34 = %d", token34);
|
This example shows the concatenation of token##n into token34. Both the stringize and the token-pasting operators are used in this example.
Parameterized Macros:
One of the powerful functions of the CPP is the ability to simulate functions using parameterized macros. For example, we might have some code to square a number:
int square(int x) {
return x * x;
}
|
We can instead rewrite this using a macro:
#define square(x) ((x) * (x))
|
Macros with arguments must be defined using the #define directive before they can be used. The argument list is enclosed in parentheses and must immediately follow the macro name. Spaces are not allowed between and macro name and open parenthesis. For example:
#define MAX(x,y) ((x) > (y) ? (x) : (y))
|
Macro Caveats:
- Macro definitions are not stored in the object file. They are only active for the duration of a single source file starting when they are defined and ending when they are undefined (using #undef), redefined, or when the end of the source file is found.
- Macro definitions you wish to use in multiple source files may be defined in an include file which may be included in each source file where the macros are required.
- When a macro with arguments is invoked, the macro processor substitutes the arguments into the macro body and then processes the results again for additional macro calls. This makes it possible, but confusing, to piece together a macro call from the macro body and from the macro arguments.
- Most experienced C programmers enclose macro arguments in parentheses when they are used in the macro body. This technique prevents undesired grouping of compound expressions used as arguments and helps avoid operator precedence rules overriding the intended meaning of a macro.
- While a macro may contain references to other macros, references to itself are not expanded. Self-referencing macros are a special feature of ANSI Standard C in that the self-reference is not interpreted as a macro call. This special rule also applies to indirectly self-referencing macros (or macros that reference themselves through another macro).
C - Useful Concepts
Error Reporting:
Many times it is useful to report errors in a C program. The standard library perror() is an easy to use and convenient function. It is used in conjunction with errno and frequently on encountering an error you may wish to terminate your program early. We will meet these concepts in other parts of the function reference chapter also.
errno: - is a special system variable that is set if a system call cannot perform its set task. It is defined in #include <errno.h>.
Predefined Streams:
UNIX defines 3 predefined streams ie. virtual files
They all use text a the method of I/O. stdin and stdout can be used with files, programs, I/O devices such as keyboard, console, etc.. stderr always goes to the console or screen.
The console is the default for stdout and stderr. The keyboard is the default for stdin.
Dynamic Memory Allocation:
Dynamic allocation is a pretty unique feature to C. It enables us to create data types and structures of any size and length to suit our programs need within the program. We use dynamic memory allocation concept when we don't know how in advance about memory requirement.
There are following functions to use for dynamic memory manipulation:
Command Line Arguments:
It is possible to pass arguments to C programs when they are executed. The brackets which follow main are used for this purpose. argc refers to the number of arguments passed, and argv[] is a pointer array which points to each argument which is passed to mainA simple example follows, which checks to see if a single argument is supplied on the command line when the program is invoked.
#include <stdio.>h
main( int argc, char *argv[] )
{
if( argc == 2 )
printf("The argument supplied is %s\n", argv[1]);
else if( argc > 2 )
printf("Too many arguments supplied.\n");
else
printf("One argument expected.\n");
}
|
Note that *argv[0] is the name of the program invoked, which means that *argv[1] is a pointer to the first argument supplied, and *argv[n] is the last argument. If no arguments are supplied, argc will be one. Thus for n arguments, argc will be equal to n + 1. The program is called by the command line:
More clearly, Suppose a program is compiled to an executable program myecho and that the program is executed with the following command.
When this command is executed, the command interpreter calls the main() function of the myprog program with 4 passed as the argc argument and an array of 4 strings as the argv argument.
argv[0] - "myprog"
argv[1] - "aaa"
argv[2] - "bbb"
argv[3] - "ccc"
|
Multidimensional Arrays:
The array we used in the last example was a one dimensional array. Arrays can have more than one dimension, these arrays-of-arrays are called multidimensional arrays. They are very similar to standard arrays with the exception that they have multiple sets of square brackets after the array identifier. A two dimensional array can be though of as a grid of rows and columns.
#include <stdio.h>
const int num_rows = 7;
const int num_columns = 5;
int
main()
{
int box[num_rows][num_columns];
int row, column;
for(row = 0; row < num_rows; row++)
for(column = 0; column < num_columns; column++)
box[row][column] = column + (row * num_columns);
for(row = 0; row < num_rows; row++)
{
for(column = 0; column < num_columns; column++)
{
printf("%4d", box[row][column]);
}
printf("\n");
}
return 0;
}
|
This will produce following result:
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24
25 26 27 28 29
30 31 32 33 34
|
The above array has two dimensions and can be called a doubly subscripted array. GCC allows arrays of up to 29 dimensions although actually using an array of more than three dimensions is very rare. |
C - Built-in Library Functions
String Manipulation Functions
- char *strcpy (char *dest, char *src); Copy src string into dest string.
- char *strncpy(char *string1, char *string2, int n); Copy first n characters of string2 to stringl .
- int strcmp(char *string1, char *string2); Compare string1 and string2 to determine alphabetic order.
- int strncmp(char *string1, char *string2, int n); Compare first n characters of two strings.
- int strlen(char *string); Determine the length of a string.
- char *strcat(char *dest, const char *src); Concatenate string src to the string dest.
- char *strncat(char *dest, const char *src, int n); Concatenate n chracters from string src to the string dest.
- char *strchr(char *string, int c); Find first occurrence of character c in string.
- char *strrchr(char *string, int c); Find last occurrence of character c in string.
- char *strstr(char *string2, char string*1); Find first occurrence of string string1 in string2.
- char *strtok(char *s, const char *delim) ; Parse the string s into tokens using delim as delimiter.
Memory Management Functions
Buffer Manipulation
- void* memcpy(void* s, const void* ct, int n); Copies n characters from ct to s and returns s. s may be corrupted if objects overlap.
- int memcmp(const void* cs, const void* ct, int n); Compares at most (the first) n characters of cs and ct, returning negative value if cs<ct, zero if cs==ct, positive value if cs>ct.
- void* memchr(const void* cs, int c, int n); Returns pointer to first occurrence of c in first n characters of cs, or NULL if not found.
- void* memset(void* s, int c, int n); Replaces each of the first n characters of s by c and returns s.
- void* memmove(void* s, const void* ct, int n); Copies n characters from ct to s and returns s. s will not be corrupted if objects overlap.
Character Functions
Error Handling Functions
BIT MANIPULATIONS
Working on bytes, or data types comprising of bytes like ints, floats, doubles or even data structures which stores large amount of bytes is normal for a programmer. In some cases, a programmer needs to go beyond this - that is to say that in a deeper level where the importance of bits is realized.
Operations with bits are used in Data compression (data is compressed by converting it from one representation to another, to reduce the space) ,Exclusive-Or Encryption (an algorithm to encrypt the data for safety issues). In order to encode, decode or compress files we have to extract the data at bit level. Bitwise Operations are faster and closer to the system and sometimes optimize the program to a good level.
We all know that 1 byte comprises of 8 bits and any integer or character can be represented using bits in computers, which we call its binary form(contains only 1 or 0) or in its base 2 form.
Example:
1) 14 = {1110 }2
= 1 * 23 + 1 * 22 + 1 * 21 + 0 * 20
= 14.
2) 20 = {10100 }2
= 1 * 24 + 0 * 23 + 1 * 22 + 0 * 21 + 0 * 20
= 20.
For characters, we use ASCII representation, which are in the form of integers which again can be represented using bits as explained above.
Bitwise Operators:
There are different bitwise operations used in the bit manipulation. These bit operations operate on the individual bits of the bit patterns. Bit operations are fast and can be used in optimizing time complexity. Some common bit operators are:
NOT ( ~ ): Bitwise NOT is an unary operator that flips the bits of the number i.e., if the ith bit is 0, it will change it to 1 and vice versa. Bitwise NOT is nothing but simply the one’s complement of a number. Lets take an example.
N = 5 = (101)2
~N = ~5 = ~(101)2 = (010)2 = 2
AND ( & ): Bitwise AND is a binary operator that operates on two equal-length bit patterns. If both bits in the compared position of the bit patterns are 1, the bit in the resulting bit pattern is 1, otherwise 0.
A = 5 = (101)2 , B = 3 = (011)2 A & B = (101)2 & (011)2= (001)2 = 1
OR ( | ): Bitwise OR is also a binary operator that operates on two equal-length bit patterns, similar to bitwise AND. If both bits in the compared position of the bit patterns are 0, the bit in the resulting bit pattern is 0, otherwise 1.
A = 5 = (101)2 , B = 3 = (011)2
A | B = (101)2 | (011)2 = (111)2 = 7
XOR ( ^ ): Bitwise XOR also takes two equal-length bit patterns. If both bits in the compared position of the bit patterns are 0 or 1, the bit in the resulting bit pattern is 0, otherwise 1.
A = 5 = (101)2 , B = 3 = (011)2
A ^ B = (101)2 ^ (011)2 = (110)2 = 6
Left Shift ( << ): Left shift operator is a binary operator which shift the some number of bits, in the given bit pattern, to the left and append 0 at the end. Left shift is equivalent to multiplying the bit pattern with 2k ( if we are shifting k bits ).
1 << 1 = 2 = 21
1 << 2 = 4 = 22 1 << 3 = 8 = 23
1 << 4 = 16 = 24
…
1 << n = 2n
Right Shift ( >> ): Right shift operator is a binary operator which shift the some number of bits, in the given bit pattern, to the right and append 1 at the end. Right shift is equivalent to dividing the bit pattern with 2k ( if we are shifting k bits ).
4 >> 1 = 2
6 >> 1 = 3
5 >> 1 = 2
16 >> 4 = 1
Bitwise operators are good for saving space and sometimes to cleverly remove dependencies.
Note: All left and right side taken in this article, are taken with reference to the reader.
Lets discuss some algorithms based on bitwise operations:
1) How to check if a given number is a power of 2 ?
Consider a number N and you need to find if N is a power of 2. Simple solution to this problem is to repeated divide N by 2 if N is even. If we end up with a 1 then N is power of 2, otherwise not. There are a special case also. If N = 0 then it is not a power of 2. Let’s code it.
Implementation:
bool isPowerOfTwo(int x)
{
if(x == 0)
return false;
else
{
while(x % 2 == 0) x /= 2;
return (x == 1);
}
}
Above function will return true if x is a power of 2, otherwise false.
Time complexity of the above code is O(logN).
The same problem can be solved using bit manipulation. Consider a number x that we need to check for being a power for 2. Now think about the binary representation of (x-1). (x-1) will have all the bits same as x, except for the rightmost 1 in x and all the bits to the right of the rightmost 1.
Let, x = 4 = (100)2
x - 1 = 3 = (011)2
Let, x = 6 = (110)2
x - 1 = 5 = (101)2
It might not seem obvious with these examples, but binary representation of (x-1) can be obtained by simply flipping all the bits to the right of rightmost 1 in x and also including the rightmost 1.
Now think about x & (x-1). x & (x-1) will have all the bits equal to the x except for the rightmost 1 in x.
Let, x = 4 = (100)2
x - 1 = 3 = (011)2
x & (x-1) = 4 & 3 = (100)2 & (011)2 = (000)2
Let, x = 6 = (110)2
x - 1 = 5 = (101)2
x & (x-1) = 6 & 5 = (110)2 & (101)2 = (100)2
Properties for numbers which are powers of 2, is that they have one and only one bit set in their binary representation. If the number is neither zero nor a power of two, it will have 1 in more than one place. So if x is a power of 2 then x & (x-1) will be 0.
Implementation:
bool isPowerOfTwo(int x)
{
// x will check if x == 0 and !(x & (x - 1)) will check if x is a power of 2 or not
return (x && !(x & (x - 1)));
}
2) Count the number of ones in the binary representation of the given number.
The basic approach to evaluate the binary form of a number is to traverse on it and count the number of ones. But this approach takes log2N of time in every case.
Why log2N ?
As to get a number in its binary form, we have to divide it by 2, until it gets 0, which will take log2N of time.
With bitwise operations, we can use an algorithm whose running time depends on the number of ones present in the binary form of the given number. This algorithm is much better, as it will reach to logN, only in its worst case.
int count_one (int n)
{
while( n )
{
n = n&(n-1);
count++;
}
return count;
}
Why this algorithm works ?
As explained in the previous algorithm, the relationship between the bits of x and x-1. So as in x-1, the rightmost 1 and bits right to it are flipped, then by performing x&(x-1), and storing it in x, will reduce x to a number containing number of ones(in its binary form) less than the previous state of x, thus increasing the value of count in each iteration.
Example:
n = 23 = {10111}2 .
1. Initially, count = 0.
2. Now, n will change to n&(n-1). As n-1 = 22 = {10110}2 , then n&(n-1) will be {101112 & {10110}2, which will be {10110}2 which is equal to 22. Therefore n will change to 22 and count to 1.
3. As n-1 = 21 = {10101}2 , then n&(n-1) will be {10110}2 & {10101}2, which will be {10100}2 which is equal to 20. Therefore n will change to 20 and count to 2.
4. As n-1 = 19 = {10011}2 , then n&(n-1) will be {10100}2 & {10011}2, which will be {10000}2 which is equal to 16. Therefore n will change to 16 and count to 3.
5. As n-1 = 15 = {01111}2 , then n&(n-1) will be {10000}2 & {01111}2, which will be {00000}2 which is equal to 0. Therefore n will change to 0 and count to 4.
6. As n = 0, the the loop will terminate and gives the result as 4.
Complexity: O(K), where K is the number of ones present in the binary form of the given number.
3) Check if the ith bit is set in the binary form of the given number.
To check if the ith bit is set or not (1 or not), we can use AND operator. How?
Let’s say we have a number N, and to check whether it’s ith bit is set or not, we can AND it with the number 2i . The binary form of 2i contains only ith bit as set (or 1), else every bit is 0 there. When we will AND it with N, and if the ith bit of N is set, then it will return a non zero number (2i to be specific), else 0 will be returned.
Using Left shift operator, we can write 2i as 1 << i . Therefore:
bool check (int N)
{
if( N & (1 << i) )
return true;
else
return false;
}
Example:
Let’s say N = 20 = {10100}2. Now let’s check if it’s 2nd bit is set or not(starting from 0). For that, we have to AND it with 22 = 1<<2 = {100}2 .
{10100} & {100} = {100} = 22 = 4(non-zero number), which means it’s 2nd bit is set.
4) How to generate all the possible subsets of a set ?
A big advantage of bit manipulation is that it can help to iterate over all the subsets of an N-element set. As we all know there are 2N possible subsets of any given set with N elements. What if we represent each element in a subset with a bit. A bit can be either 0 or 1, thus we can use this to denote whether the corresponding element belongs to this given subset or not. So each bit pattern will represent a subset.
Consider a set A of 3 elements.
A = {a, b, c}
Now, we need 3 bits, one bit for each element. 1 represent that the corresponding element is present in the subset, whereas 0 represent the corresponding element is not in the subset. Let’s write all the possible combination of these 3 bits.
0 = (000)2 = {}
1 = (001)2 = {c}
2 = (010)2 = {b}
3 = (011)2 = {b, c}
4 = (100)2 = {a}
5 = (101)2 = {a, c}
6 = (110)2 = {a, b}
7 = (111)2 = {a, b, c}
Pseudo Code:
possibleSubsets(A, N):
for i = 0 to 2^N:
for j = 0 to N:
if jth bit is set in i:
print A[j]
print ‘\n’
Implementation:
void possibleSubsets(char A[], int N)
{
for(int i = 0;i < (1 << N); ++i)
{
for(int j = 0;j < N;++j)
if(i & (1 << j))
cout << A[j] << ‘ ‘;
cout << endl;
}
}
5) Find the largest power of 2 (most significant bit in binary form), which is less than or equal to the given number N.
Idea: Change all the bits which are at the right side of the most significant digit, to 1.
Property: As we know that when all the bits of a number N are 1, then N must be equal to the 2i -1 , where i is the number of bits in N.
Example:
Let’s say binary form of a N is {1111}2 which is equal to 15.
15 = 24-1, where 4 is the number of bits in N.
This property can be used to find the largest power of 2 less than or equal to N. How?
If we somehow, change all the bits which are at right side of the most significant bit of N to 1, then the number will become x + (x-1) = 2 * x -1 , where x is the required answer.
Example:
Let’s say N = 21 = {10101}, here most significant bit is the 4th one. (counting from 0th digit) and so the answer should be 16.
So lets change all the right side bits of the most significant bit to 1. Now the number changes to
{11111} = 31 = 2 * 16 -1 = Y (let’s say).
Now the required answer is (Y+1)>>1 or (Y+1)/2.
Now the question arises here is how can we change all right side bits of most significant bit to 1?
Let’s take the N as 16 bit integer and binary form of N is {1000000000000000}.
Here we have to change all the right side bits to 1.
Initially we will copy that most significant bit to its adjacent right side by:
N = N | (N>>1).
As you can see, in above diagram, after performing the operation, rightmost bit has been copied to its adjacent place.
Now we will copy the 2 rightmost set bits to their adjacent right side.
N = N | (N>>2).
Now we will copy the 4 rightmost set bit to their adjacent right side.
N = N | (N>>4)
Now we will copy these 8 rightmost set bits to their adjacent right side.
N = N| (N>>8)
Now all the right side bits of the most significant set bit has been changed to 1 .This is how we can change right side bits. This explanation is for 16 bit integer, and it can be extended for 32 or 64 bit integer too.
Implementation:
long largest_power(long N)
{
//changing all right side bits to 1.
N = N| (N>>1);
N = N| (N>>2);
N = N| (N>>4);
N = N| (N>>8);
//as now the number is 2 * x-1, where x is required answer, so adding 1 and dividing it by
2.
return (N+1)>>1;
}
Tricks with Bits:
1) x ^ ( x & (x-1)) : Returns the rightmost 1 in binary representation of x.
As explained above, (x & (x - 1)) will have all the bits equal to the x except for the rightmost 1 in x. So if we do bitwise XOR of x and (x & (x-1)), it will simply return the rightmost 1. Let’s see an example.
x = 10 = (1010)2
x & (x-1) = (1010)2 & (1001)2 = (1000)2
x ^ (x & (x-1)) = (1010)2 ^ (1000)2 = (0010)2
2) x & (-x) : Returns the rightmost 1 in binary representation of x
(-x) is the two’s complement of x. (-x) will be equal to one’s complement of x plus 1.
Therefore (-x) will have all the bits flipped that are on the left of the rightmost 1 in x. So x & (-x) will return rightmost 1.
x = 10 = (1010)2
(-x) = -10 = (0110)2
x & (-x) = (1010)2 & (0110)2 = (0010)2
3) x | (1 << n) : Returns the number x with the nth bit set.
(1 << n) will return a number with only nth bit set. So if we OR it with x it will set the nth bit of x.
x = 10 = (1010)2 n = 2
1 << n = (0100)2
x | (1 << n) = (1010)2 | (0100)2 = (1110)2
Applications of bit operations:
1) They are widely used in areas of graphics ,specially XOR(Exclusive OR) operations.
2) They are widely used in the embedded systems, in situations, where we need to set/clear/toggle just one single bit of a specific register without modifying the other contents. We can do OR/AND/XOR operations with the appropriate mask for the bit position.
3) Data structure like n-bit map can be used to allocate n-size resource pool to represent the current status.
4) Bits are used in networking, framing the packets of numerous bits which is sent to another system generally through any type of serial interface.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
i will update this page
until then see this link for more info
http://www.learn-c.org/
|
|
|
|
|
|
|
|
No comments:
Post a Comment
commnet here