Bitwise Operators
Bitwise Operators
One major use ofthe bitwise AND, &, and the bitwise OR,|, is in operations to test and set individual bits in an
integer variable
can use individual bits to store data that involve one of two choices
You could use a single integer variable to store several characteristics of a person.
store whether the person is male or female withone bit
use three other bits to specify whether the person can speak French, German, or Italian
another bit to record whether the person's salary is $50,000 or more
in just four bits you have a substantial set of data recorded
CFOR BEGINNERs
Bitwise Qperators ean Coodemy
Binary Numbers
abinary number is anumber that includes only ones and zeroes.
10101
1 0101010
10 1011110101
01 0110101110
111000 000111
CFOR BEGINNERS
in general, the "position values" in a binary number are the powers of two.
CFOR BEGINNERS
128 64 32 16 8 2 1
1 1 1 1
1X1 =1
OX2 =0
OX4 =0
1X8 =8
OX 16 =0
1X32 =32
1X 64 = 64
OX128 =0
Answer: 105
6
Bitwise Operators (tutorials point)
Operator Description Example
Binary AND Operator copies abit to the result if it (A & B) = 12, i.e., 0000 1100
exists in both operands.
Binary OR Operator copies a bit if it exists in either (A | B) = 61, i.e., 0011 1101
operand.
BinaryXOR Operator copies the bit if it is set in one (A ^B) =49, i.e., 0011 0001
operand but not both.
Binary Ones Complement Operator is unary and has (~A)= -61, i.e,. 1100 0011
the effect of flipping' bits. 2's complement form.
<< Binary Left Shift Operator. The left operands value is
moved left by the number of bits specified by the right A<< 2 = 240 i.e., 1111 0000
operand.
test.c X
6
7
#include <stdio.h>
9
10 int main ()
11 {
12 unsigned int a = 60;
13 unsigned int b = 13;
14 int result = 0;
15
16 result = a & b;
17
18 printf ("result is %d", result);
19
20 return 0;
21
<
Logs &others
result is 12
Process returned e (ØxO)
Press any key to continL
Type Conversions
"conversion of data between different types can happen automatically (implicit conversion) by the
language or explicit by the program
"to effectively develop Cprograms, you must understand the rules used for the implicit conversion
of floating-point and integer values in C
"Normally, you shouldn't mix types, but there are occasions when it is useful
"remember, c is flexible, gives you the freedom, but, do not abuse it
"Whenever a floating-point value is assigned to an integer variable in C, the decimal portion of the
number gets truncated
int x = 0;
float f =,12.125;
X=f: || value stored in x is the number 12, onlythe int portion is stored
CFOR BEGINNERS
The Cast and sizeof operators
3
Type Conversion (cont'd)
"assigning an integer variable to a floating variable does not cause anychange in the value of the
number
"value is converted by the system andstored in the floating variable
"If one operand is an int and the other is a float then the operation is performed as a floating
point operation
CFOR BECINNFRS
The Cast and sizeof operators
The Cast Operator
"As mentioned, you should usually steer clear of automatic type conversions, especially of demotions
"better to do an explicit conversion
"it is possible for you to demand the precise type conversion that you want
"called a cast and consists of preceding the quantity with the name of the desired type in parentheses
*parentheses and type name together constitute a cast operator, i.e. (type)
"The actual type desired, suchas long, is substituted for the word type
CFOR BEGINNERS
The Cast and sizeof operators {}eon
The Cast Operator
"The type cast operator has a higher precedence than all the arithmetic operators except the unary
minus and unary plus
21 + 26
CFOR BEGINNERS
The Cast and sizeof operators {}lean CocemY
sizeof operator
"sizeof(int) will result in the number of bytes occupied by a variable of type int
"Use the sizeof operator wherever possible to avoid having to calçulate and hard-code sizes into your
program
CFOR BEGINNFRS
The Cast and sizeof operators {}ion