Skip to content

Instantly share code, notes, and snippets.

@dlivingstone
Created November 8, 2011 10:37
Show Gist options
  • Save dlivingstone/1347460 to your computer and use it in GitHub Desktop.
Save dlivingstone/1347460 to your computer and use it in GitHub Desktop.
Bitshifts and bitfields: A simple demo program to illustrate bitshifting and the use of bitfields in C++
// Annotated C++ program to illustrate use of bitshift
// operators and bitfields
// CC-BY-SA Daniel Livingstone
// University of the West of Scotland
// Preprocessor directive to include the i/o library:
#include <iostream>
// i/o functions are part of the standard library
// & we are using the standard library 'namespace' in this program
using namespace std;
// define values for pizza toppings
// these hex values equate to binary
// 00000001, 00000010, 00000100, 00001000, etc...
#define PEPPERONI 0x001
#define HAM 0x002
#define PINEAPPLE 0x004
#define CHICKEN 0x008
#define PEPPERS 0x010
#define MUSHROOM 0x020
#define ONION 0x040
#define ANCHOVY 0x080
// A simple function to get an integer from console input with some basic error checking
// Does *not* check whether an integer is within a particular range of values
// but does clear the input stream when the user enters text instead of numbers
// Invalid input is replaced with a 1
int getInt(void)
{
int i;
cin >> i;
if (cin.fail() )
{
cout << endl << "Invalid type entered, substituting with '1'." << endl;
i = 1;
}
cin.clear();
cin.ignore(INT_MAX,'\n');
return i;
}
// bitfields
// Using the #defined constants (above), determine what collection of pizza toppings is represented
// in a single byte integer value. A byte can have a value between 0 and 255.
// If the user enters an integer greater than 255, the lowest value byte will be retained, the
// values over 255 discarded (256 becomes 0, 257 becomes 1, etc.)
void bitfields(void)
{
cout << "Enter value between 1 and 255 to choose your pizza toppings! (0 to exit)" << endl;
unsigned char x = (unsigned char)getInt();
while (x != 0) {
char toppings = (x & 255);
cout << " Your toppings: " << endl;
if (x & PEPPERONI) cout << "pepperoni ";
if (x & HAM) cout << "ham ";
if (x & PINEAPPLE) cout << "pineapple ";
if (x & CHICKEN) cout << "chicken ";
if (x & PEPPERS) cout << "peppers ";
if (x & MUSHROOM) cout << "mushroom ";
if (x & ONION) cout << "onion ";
if (x & ANCHOVY) cout << "anchovy ";
cout << endl << endl;
cout << "enter value between 1 and 255 to choose your pizza toppings! (0 to exit)" << endl;
x = (unsigned char)getInt();
}
}
// bitshifting
// This function demonstrates the use of the shift operators (<< and >>) on integers
// and compares with multiplication and division by two. Inspection of the machine code
// generated by this function should reveal that modern compilers automatically replace
// multiplication or division by two with shift operations - but that some differences
// remain.
// Try entering negative values to see what happens!
void bitshifting(void)
{
cout << "Enter value for x (0 to exit)" << endl;
cout << "Test both positive and negative values." << endl;
int x = getInt();
while (x != 0) {
cout << "x * 2 and x / 2: " << endl;
int a = x*2;
int b = x/2;
cout << a << " " << b << endl << endl;
cout << "x << 1 and x >> 1: " << endl;
a = (x<<1);
b = (x>>1);
cout << a << " " << b << endl << endl;
cout << "Enter value for x (0 to exit)" << endl;
x = getInt();
}
return;
}
// main() is the program entry point in C and C++
// call bitfields and them bitshifting
int main()
{
bitfields();
bitshifting();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment