






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A program is a precise sequence of steps to solve a particular problem. This course includes basic programming structure like loops, operator, memory allocation, reference, pointers etc. It teaches how to be a good programmer. This lecture handout is about: Bit, Manipulation, Operator, AND, OR, Exclsuive, OR, NOT, Flags, Masking, Unsigned, Integers, Shift, Operator, Attributes, Truth, Table
Typology: Study notes
1 / 10
This page cannot be seen from the preview
Don't miss anything!







Reading Material
Deitel & Deitel - C++ How to Program Chapter. 16
Summary Bit Manipulation Bit Manipulation Operators AND Operator OR Operator Exclusive OR Operator NOT Operator Bit Flags Masking Unsigned Integers Sample Program Shift Operators
Bit Manipulation We have so far been dealing with bytes using different data types. In this lecture, we will see what a bit is? Bit is the basic unit of memory. Eight bits form a byte. As you know that data is stored in computers in 0ās and 1ās form. An integer uses four bytes and the integer calculations occur in four bytes. Thus, we are manipulating bytes while using different data types. Now we will try to understand the process of ābit manipulationā. Now we will deal with each bit in a byte and explore how to do on or off each bit. A bit, having 1 is said on while the one with 0 is called off. Here we will discuss different operators to manipulate bits. The concept of bit manipulation means that we can do work with a bit, the smallest unit of memory. Bit manipulations utilize very small memory. Thus, we can make an efficient use of the memory. The bit fields are of great use in operating systems and files attributes. The bit manipulations are useful while working at operating system level. Letās have a look on different operators, used for bit manipulations.
Bit Manipulation Operators The following table shows different operators used for bit manipulation.
Operator Operator Sign Bitwise AND Operator & Bitwise OR Operator | Bitwise Exclusive OR Operator
NOT Operator ~ Left Shift Operator <<
Right Shift Operator >>
Here & is the bit-wise AND operator. Donāt confuse it with the logical AND operator &&. Similarly | is the bit-wise OR operator. Donāt confuse it with the logical OR operator ||.
Now letās talk about these operators in detail.
AND Operator ( & ) The AND operator (&) works just like the logical AND operator (&&) but on bits. It compares two bits and returns 1 if both bits are 1. If any of the two bits being compared is 0, the result will be 0.
Following table, also called truth table, will further explain the operation of & operator.
Bit1 Bit2 Bit1 & Bit 1 1 1 1 0 0 0 1 0 0 0 0
We know that when a number is stored in memory, it gets stored in bit pattern which has binary representation (only 1 and 0 ). So we can use & to AND two numbers bit- wise. To understand this, consider the following example. Suppose we have two numbers - 12 and 8 and want to apply & on these ones. Here we will make use of the binary number system. The binary representation (base 2 system) of 12 and 8 are as 12 = (1100) 2 and 8 = (1000) 2. Now we apply the & operator on these numbers and get the result as follows
12 = 1 1 0 0 & 8 = 1 0 0 0
1 0 0 0 Thus 12 & 8 = (1000) 2 = 8. Donāt think 12 & 8 as an arithmetic operation. It is just a bit manipulation or a pattern matching issue. Each bit of first number is matched (compared) with corresponding bit of the second number. The result of & is 1 if both bits are 1. Otherwise, it will be 0. The & operator is different from the && operator. The && operator operates on two conditions (expressions) and returns true or false while the & operator works on bits (or bit pattern) and returns a bit (or bit pattern) in 1 or 0.
Example 1 We want to determine whether in a number a specific bit is 1 or 0. Suppose we want to determine whether the fourth bit (i.e. 2^3 ) of a number is 1 or 0. We will pick the number whose fourth bit is 1 and the remaining are zero. It is 2 3 (i.e. 8). Now we will take AND of the given number with 8 (i.e 1000 in bit pattern.). In bit manipulation, the number is written in hexadecimal form. In the C language, we put 0x or 0X before
Bit1 Bit2 Bit1 | Bit 1 1 1 1 0 1 0 1 1 0 0 0
We can make it sure that a specific bit in a number should be 1 with the help of | operator. For this purpose, we take OR of this number with another number whose bit pattern has 1 in that specific bit. Then OR will produce 1 as the bit at that position in second number is 1 and OR gives 1 if any one bit is one. Thus in the output that specific bit will have 1. Let us consider the following example in which we apply OR operator on two numbers 12 and 8.
12 = 1 1 0 0 | 8 = 1 0 0 0
1 1 0 0 Hence we get 12 | 8 = 12. In case, x = 8 | 1, the OR operation will be as under.
8 = 1 0 0 0 | 1 = 0 0 0 1
1 0 0 1 Thus x = 8 | 1 = 9. Donāt take the statement in mathematical or arithmetical terms. Rather consider it from the perspective of pattern matching. The & operator is used to check whether a specific bit is set or not while the | operator is used to set a specific bit.
Exclusive OR Operator ( ^ )
Exclusive OR operator uses the sign ^. This operator returns 1 when one input is zero and the second is 1. It returns 0 if both bits are same i.e. both are either 0 or 1. The truth table of exclusive OR, also called xor (zor) , is given below.
Bit1 Bit2 Bit1 ^ Bit 1 1 0 1 0 1 0 1 1 0 0 0
To understand exclusive OR, letās work out exclusive OR of 8 and 1. In the following statement, the pattern matching is shown for 8 ^ 1.
8 = 1 0 0 0
This shows that 8 ^ 1 = 9. If we take again exclusive OR of 9 with 1. The result will be 8 again as shown below.
9 = 1 0 0 1 ^ 1 = 0 0 0 1
1 0 0 0 While taking ^ (exclusive OR) of a number with a second number and then ^ of the result with the second number, we get the first number again. This is a strength of the ^ operator that is very useful.
NOT Operator ( ~ )
This is a unary operator. It inverts the bits of the input number, meaning that if a bit of the input number is 1, the operator will change it to 0 and vice versa. The sign ~ is used for the NOT operator. Following is the truth table of the NOT operator.
Bit1 ~ Bit 1 0 0 1
Letās take NOT of the number 8. This will be as follows 8 = 1 0 0 0 Now ~8 will invert the bits from 1 to 0 and from 0 to 1. Thus ~8 will be ~8 = 0 1 1 1 which is 7.
The bit manipulation operators are very useful. Letās consider some examples to see the usefulness of these operators.
Example (Bit Flags)
The first example relates to operating system. In Windows, you can view the properties of a file. You can get the option properties by right clicking the mouse on the file name in any folder structure. You will see a window showing the properties of the file. This will show the name of the file, the date of creation/modification of the file etc. In the below part of this window, you will see some boxes with check marks. These include read only and archive etc. While looking at a check mark, you feel of having a look at a bit. If there is a check mark, it means 1. Otherwise, it will be 0. So we are looking at bit flags which will depict the status of the file. If the file is marked read-only, a specific bit is set to 1 in the operating system. This 1 indicates that the status of the file is read-only. When we look for directory in UNIX operating system, rwx, rx or rw are seen before the name of a file. The rwx are actually symbols used for read, write and execute permissions of the file. These are the attributes of the file.
working, we may place a new disk in its place. And to write a bit on this disk, we again take Exclusive OR of eight bits on the other disks and write the result on this disk. This will be the same bit that was written in the damaged disk. You can prove it by the doing the following exercise on paper. Write eight bits, take their Exclusive OR one by one and write it at ninth position. Now erase any one bit and take Exclusive OR of the remaining eight bits. You will get the same bit which was erased. Thus it is a useful technique for recovering the lost data without shutting down the system. We replace the bad disk with a new one while the system is on. The system using the RAID technique, writes the data to the new disk. This technique of replacing a disk is known as Hot Plug.
We have read the technique of swapping two numbers. In this method, we use a third temporary place to swap two numbers. Suppose a and b are to be swapped. We store a in a temporary place c. Then we store b in a and put the value of c (which has the value of a ) in b. Thus a and b are swapped. We can swap two numbers without using a third place with the help of Exclusive OR. Suppose we want to swap two unsigned numbers a and b. These can be swapped by the following three statements.
a = a ^ b ; b = b ^ a ; a = a ^ b ; Do exercises of this swap technique by taking different values of a and b.
Unsigned Integers The bit manipulations are done with unsigned integers. The most significant bit is used as a sign bit. If this bit is zero, the number is considered positive. However, if it is 1, the number will be considered negative. Normally these bit manipulations are done with unsigned integers. The unsigned integers are declared explicitly by using the word āunsignedā as follow. unsigned int i, j, k ; By this declaration the integers i, j and k will be treated as positive numbers only.
The following program demonstrate the encryption and decryption of a password. The program takes a password from user, encrypts it by using Exclusive OR ( ^) with a number. It displays the encrypted password. Then it decrypts the encrypted password using Exclusive OR ( ^ ) with the same number and we get the original password again.
Following is the code of the program. //This program demonstrate the encryption by using ^ operator
main () { char password[10] ; char *passptr ; cout << "Please enter a password(less than 10 character): " ; cin >> password ;
passptr = password ; //now encrypting the password by using ^ with 3 while (*passptr != '\0' ) { passptr = (passptr ^ 3); ++passptr ; } cout << "The encrypted password is: " << password << endl;
//now decrypting the encrypted password by using ^ with 3
passptr = password ; while (*passptr != '\0' ) { passptr = (passptr ^ 3); ++passptr ; } cout << "The decrypted password is: " << password << endl; }
The following is a sample output of the program. Please enter a password(less than 10 character): zafar The encrypted password is: ybebq The decrypted password is: zafar
Shifting the binary numbers is similar to shifting the decimal numbers. Suppose we have 1 in decimal system and want to shift it left in a way that zero is put at the ending place. Thus 1 becomes 10. Mathematically, it is a multiplication by 10. Now if we shift 10 to left and place 0 at the last place, we get 100. It is again a multiplication by 10. In pictorial terms, we can show this as under.
(In decimal system) The value is 1
Shift Left, The value is 10 (i.e. multiplication by 10)
Shift Left, The value is 100 (i.e. multiplication by 10)
1000 100 10 1 0 0 0 1
0 0 1 0
The same thing applies when we do bit shifts. If we shift a bit to the left in the binary system, it is multiplied by 2. If we do left shift again we are multiplying by 2 again. Same applies in the other direction. By shifting to the right, we will be dividing by 2 in the binary system and dividing by 10 in decimal system. In this process, the shifted digit/bit is discarded. When we do left shift, zeroes are inserted in the right side bits. The same applies to right shift, as zeros are inserted in the left side bits. But the situation will be different if we use signed numbers. As we know that in signed numbers the most significant bit is 1. Now you have to see that what happens while
Here is the out put of the program. Please enter a number: 12 The number after left shift is 24 The number after left shift again is 48 Now applying right shift The number after right shift is 6 The number after right shift again is 3
In the output, we see that the left shift operator (<<) has multiplied the number by 2 and the right shift operator (>>) has divided the number by 2. The shift operator is more efficient than direct multiplication and division.
Write different programs to demonstrate the use of bit manipulation operators. Write a program which takes two numbers, displays them in binary numbers and then displays the results of AND, OR and Exclusive OR of these numbers in binary numbers so that operations can be clearly understood. Write a program which swaps two numbers without using a temporary third variable. Write a program, which takes a password from the user, saves it to a file in encrypted form. Then allow the user to enter the password again and compare it with the stored password and show is the password valid or not.