INF212 Introduction To Programming Laboratory Leaflet: Student

You might also like

You are on page 1of 4

INF212

INTRODUCTION TO PROGRAMMING
LABORATORY LEAFLET
STUDENT

LABORATORY 3: Bitwise Operators

Task Statement

1. The example of how to make bitwise operations.

2. Using <bitset> template class.

3. Using more functionalities of <bitset> class.

1. To check whether number is even or odd by bitwise


Quiz
operator &.
Electronics Engineering INF212

Task 1: The example of how to use bitwise operations in C++.


#include <iostream>
using namespace std;

main() {
unsigned int a = 60; // 60 = 0011 1100
unsigned int b = 13; // 13 = 0000 1101
int c = 0;

c = a & b; // 12 = 0000 1100


cout << "Line 1 - Value of c is : " << c << endl ;

c = a | b; // 61 = 0011 1101
cout << "Line 2 - Value of c is: " << c << endl ;

c = a ^ b; // 49 = 0011 0001
cout << "Line 3 - Value of c is: " << c << endl ;

c = ~a; // -61 = 1100 0011


cout << "Line 4 - Value of c is: " << c << endl ;

c = a << 2; // 240 = 1111 0000


cout << "Line 5 - Value of c is: " << c << endl ;

c = a >> 2; // 15 = 0000 1111


cout << "Line 6 - Value of c is: " << c << endl ;

return 0;
}

Task 2: Using bitset template class in C++.


#include <iostream>
#include <bitset>
#include <string>

using namespace std;

int main()
{
cout << "Binary reprsentations of numbers:\n";
bitset<4> x = 32 ;
cout<<"x = " << x << endl ;

bitset<6> y = 32 ;
cout<<"y = " << y << endl ;

bitset<8> z = 32 ;
cout<<"z = " << *** << endl ;

cout<<endl;
cout<< "Conversion to integer\n" ;
cout<<"x = " << x.to_ulong() << endl;
cout<<"y = " << *********() << endl;
cout<<"z = " << z.to_ulong() << endl;

cout<<endl;
cout<< "Conversion to string\n" ;
string sx = ***********() ;
string sy = y.to_string() ;

1
Son duzenleme: 15.03.2021
Electronics Engineering INF212

string sz = z.to_string() ;

cout<<"x = " << *** << endl;


cout<<"y = " << sy << endl;
cout<<"z = " << sz << endl;

return 0;
}

Task 3: Using more functionalities of <bitset> class in C++.


#include <iostream>
#include <bitset>
using namespace std;

int main(){

******<4> b; // initialization by element


*[0] = 1 ; //true
b[1] = 0 ; //false
*[2] = 1 ; //true
b[3] = 1 ; // true

cout<<"b : "<< b << endl << endl ;

cout <<"number of set bits: " << b.count() << endl ;

cout <<"number of total bits: " << b.size() << endl;

cout <<"number of reset bits " << b.size() - b.count()


<< endl << endl ;

cout <<"inverts bits to opposite\n";


b.flip();
cout<<"b : "<< b << endl<< endl ;

cout <<"1 left shift\n";


b = b << 1;
cout<<"b : "<< b << endl<< endl ;

cout <<"2 right shift\n";


b = b *** 2;
cout<<"b : "<< b << endl<< endl ;

cout <<"set the third bit \n ";


b = b.set(*);
cout<<"b : "<< b << endl<< endl ;

cout <<" reset the first bit \n ";


b = b.*****(0);
cout<<"b : "<< b << endl<< endl ;

cout <<" set all the bits to 0 \n ";


b = b.*****();
cout<<"b : "<< b << endl << endl;

cout <<" set all the bits to 1 \n ";


b = b.****();

2
Son duzenleme: 15.03.2021
Electronics Engineering INF212

cout<<"b : "<< *** << endl<< endl ;

return 0;
}

Quiz

FOY HAKKINDA:

Isbu foy Gebze Teknik Universitesi Elektronik Muhendisligi Bolumunde verilen


CSE112 Introduction to Programming dersi laboratuvari icin hazirlanmistir.

Islem Yapan
Hazirlama
Emrah Sever
22.05.2018
Revizyon
Süleyman TUNÇEL
15.03.2021

3
Son duzenleme: 15.03.2021

You might also like