0% found this document useful (0 votes)
7 views

Bitwise Operators

C programming bitwise operators
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Bitwise Operators

C programming bitwise operators
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Overview

Coffers bitwise logical operators and shift operators


look something like the logical operators yousaw earlier but are quite different
operate on the bits in integer values

Not used in the common program

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.

the number could be of any length

the following are all examples of binary numbers

10101

1 0101010

10 1011110101

01 0110101110

111000 000111

CFOR BEGINNERS

Bitwise Qrperators {}ecn COodemy


Binary Numbers
each position for abinary number has avalue.

for each digit, multiply the digit by its position value

add up all of the products to get the final result

in general, the "position values" in a binary number are the powers of two.

The first positionvalue is 20, i.e. one


The 2nd position value is 21, i.e. two
The 2nd position value is 22, i.e. four
The 2nd position value is 23, i.e. eight
The 2nd position value is 24, i.e. sixteen
etc.

CFOR BEGINNERS

Bitwise Operators {LeanooOoodemy


Example
" Thevalue of binary 01101001 is decimal 105. This is worked out below:

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.

Binary Right Shift Operator. The left operands value is


moved right by the number of bits specified by the A>> 2= 15 i.e.,0000 1111
right operand.
de::Blocks 16.01
earch Project Build Debug Fortran wxSmith Tools Tools+ Plugins DoxyBlocks Settings Help
Debug <gobal> main() : int
** *<

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

"when performing integer arithmetic


"if two operands in an expression are integers then any decimal portion resulting from a division
operation is discarded, even if the result is assigned to a 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

(int) 21.51 + (int) 26.99


" is evaluated in C as

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

" Youcan also apply the sizeof operator to an expression


"result is the size of the value that results from evaluating the expression

"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

You might also like