Exercise 3-13 (Bitwise operations, rotation, print byte in binary)
Chapter_3 Exercise_3-12 Bitwise | Exercise_3-14 |
Exercise 3-13 TCP1, p. 228
Exercise 3-13. Modify Bitwise.cpp (from ch3-Bitwise) to use the functions from Rotation.cpp (see below). Make sure you display the results in such a way that it’s clear what’s happening during rotations.
Note: See ch3-Bitwise for PrintBinary.hpp and PrintBinary.cpp.
CONTENTS: Rotation.hpp Rotation.cpp Bitwiser.cpp
Rotation.hpp TCP1, p. 175-176 download
// Perform left and right rotations
// rotate one position to the left:
unsigned char rol(unsigned char val);
// rotate one position to the right:
unsigned char ror(unsigned char val);
Rotation.cpp TCP1, p. 175-176 download
// Perform left and right rotations
// rotate one position to the left:
unsigned char rol(unsigned char val)
{
int highbit;
if(val & 0x80) // 0x80 is the high bit only: 10000000
{highbit = 1;}
else {highbit = 0;}
// Left shift (bottom bit becomes 0):
val <<= 1;
// Rotate the high bit onto the bottom:
val |= highbit;
return val;
}
// rotate one position to the right:
unsigned char ror(unsigned char val)
{
int lowbit;
if(val & 1) // Check the low bit: 00000001
{lowbit = 1;}
else {lowbit = 0;}
// Right shift by one position:
val >>= 1; // highest bit becomes 0 (for unsigned)
// Rotate the low bit onto the top:
val |= (lowbit << 7);
return val;
}
/*
g++ -c Rotation.cpp // create object file
*/
Bitwiser.cpp download
// Demonstration of bit manipulation
#include "PrintBinary.hpp" // for printBinary()
#include "Rotation.hpp" // for rol(), ror()
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
// A macro to save typing:
#define PR(STR, EXPR) \
cout << STR; printBinary(EXPR); cout << endl;
int main()
{
unsigned int getval;
unsigned char a, b;
cout << "Enter a number between 0 and 255: ";
cin >> getval; a = getval;
PR("rol(a) = ", rol(a)) // semicolon not required here
PR("a in binary: ", a)
PR("ror(a) = ", ror(a))
cout << endl;
cout << "Enter a number between 0 and 255: ";
cin >> getval; b = getval;
PR("rol(b) = ", rol(b))
PR("b in binary: ", b)
PR("ror(b) = ", ror(b))
cout << endl;
PR("rol(a | b) = ", rol(a | b))
PR("a | b = ", a | b)
PR("ror(a | b) = ", ror(a | b))
cout << endl;
PR("rol(a & b) = ", rol(a & b))
PR("a & b = ", a & b)
PR("ror(a & b) = ", ror(a & b))
cout << endl;
PR("rol(a ^ b) = ", rol(a ^ b))
PR("a ^ b = ", a ^ b)
PR("ror(a ^ b) = ", ror(a ^ b))
cout << endl;
PR("rol(~a) = ", rol(~a))
PR("~a = ", ~a)
PR("ror(~a) = ", ror(~a))
cout << endl;
PR("rol(~b) = ", rol(~b))
PR("~b = ", ~b)
PR("ror(~b) = ", ror(~b))
cout << endl;
// An interesting bit pattern:
unsigned char c = 0x5A; // 90
PR("rol(c) = ", rol(c))
PR("c in binary: ", c)
PR("ror(c) = ", ror(c))
cout << endl;
a |= c;
cout << "a |= c;" << endl;
PR("rol(a) = ", rol(a))
PR("a = ", a)
PR("ror(a) = ", ror(a))
cout << endl;
b &= c;
cout << "b &= c;" << endl;
PR("rol(b) = ", rol(b))
PR("b = ", b)
PR("ror(b) = ", ror(b))
cout << endl;
b ^= a;
cout << "b ^= a;" << endl;
PR("rol(b) = ", rol(b))
PR("b = ", b)
PR("ror(b) = ", ror(b))
}
/*
g++ -c PrintBinary.cpp Rotation.cpp Bitwiser.cpp // create object files
g++ -c *.cpp
g++ PrintBinary.o Rotation.o Bitwiser.o -o Bitwiser // link object files,
g++ *.o -o Bitwiser // create executable
rm *.o // clean (delete object files)
./Bitwiser
Enter a number between 0 and 255: 26
rol(a) = 00110100
a in binary: 00011010
ror(a) = 00001101
Enter a number between 0 and 255: 105
rol(b) = 11010010
b in binary: 01101001
ror(b) = 10110100
rol(a | b) = 11110110
a | b = 01111011
ror(a | b) = 10111101
rol(a & b) = 00010000
a & b = 00001000
ror(a & b) = 00000100
rol(a ^ b) = 11100110
a ^ b = 01110011
ror(a ^ b) = 10111001
rol(~a) = 11001011
~a = 11100101
ror(~a) = 11110010
rol(~b) = 00101101
~b = 10010110
ror(~b) = 01001011
rol(c) = 10110100
c in binary: 01011010
ror(c) = 00101101
a |= c;
rol(a) = 10110100
a = 01011010
ror(a) = 00101101
b &= c;
rol(b) = 10010000
b = 01001000
ror(b) = 00100100
b ^= a;
rol(b) = 00100100
b = 00010010
ror(b) = 00001001
*/
Chapter_3 Exercise_3-12 Bitwise | BACK_TO_TOP | Exercise_3-14 |
Comments
Post a Comment