UB CS 400 - Handout Bit Processing and Masking

Unformatted text preview:

Handout Bit Processing and MaskingBitData ImplementationOutputHandout Bit Processing and Masking#ifndef _BITDATA#define _BITDATAclass BitData {public:void setID(int id);int getID();void setColor1(int color);int getColor1();void setColor2(int color);int getColor2();private:unsigned char data [4]; };#endifBitData class header fileDriver program#include <iostream.h>#include "BitData.h"int main() {BitData data;data.setID(2354);cout << "ID = " << data.getID() << endl;data.setID(123123);cout << "ID = " << data.getID() << endl;data.setColor1(15);cout << "Color1 = " << data.getColor1() << endl;data.setColor1(12);cout << "Color1 = " << data.getColor1() << endl;/data.setColor2(7);cout << "Color2 = " << data.getColor2() << endl;data.setColor2(1);cout << "Color2 = " << data.getColor2() << endl;return 0; }#include "BitData.h"void BitData::setID(int id) {unsigned int * p = (unsigned int *)data;*p = *p & 0x00000fff; // clear the ID field (leave colors 1-3)id = (id << 12) & 0xfffff000; // id shifted with trailing zeros*p = *p | id; // put into data storage member }int BitData::getID() {unsigned int * p = (unsigned int *)data;return (*p) >> 12; }void BitData::setColor1(int color) {if (color > 15) return;data[2] = data[2] & (char)0xf0; // clear out low nibble of byte #3unsigned char mask =(unsigned char)0x0f & (unsigned char)color;data[2] = data[2] | mask; }int BitData::getColor1() {return (unsigned int)((unsigned char)0x0f & data[2]); }void BitData::setColor2(int color) {if (color > 15) return;data[3] = data[3] & (char)0x0f; // clear out hi nibble of byte #3unsigned char mask =(unsigned char)0xf0 & ((unsigned char)color << 4);data[3] = data[3] | mask; }int BitData::getColor2() {return (unsigned int)(data[3] >> 4); }BitData ImplementationOutputindia% a.outID = 2354ID = 123123Color1 = 15Color1 = 12Color2 = 7Color2 =


View Full Document

UB CS 400 - Handout Bit Processing and Masking

Download Handout Bit Processing and Masking
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Handout Bit Processing and Masking and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Handout Bit Processing and Masking 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?