Source Code Bit´s and Bytes

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

HalfWayToHell333

Jr.Coder
Silenced
Full Member
Nobleman
Jun 23, 2014
80
783
3
Hey there,
while educating myself in Bitsetting , i have written a little Programm i would like to share. It is self explaining.
Using the | operator and & operator for bitsetting and checking.
Hope it´s usefull for someone.
C++:
#include <iostream>

namespace GlobalEnums{
	enum{//enums from 1 to 16 bit´s
		bitFlag1 = 0x1,
		bitFlag2 = 0x2,
		bitFlag4 = 0x4,
		bitFlag8 = 0x8,
		bitFlag16 = 0x10,
		bitFlag32 = 0x20,
		bitFlag64 = 0x40,
		bitFlag128 = 0x80,
		bitFlag256 = 0x100,
		bitFlag512 = 0x200,
		bitFlag1024 = 0x400,
		bitFlag2048 = 0x800,
		bitFlag4096 = 0x1000,
		bitFlag8192 = 0x2000,
		bitFlag16384 = 0x4000,
		bitFlag32768 = 0x8000,
		bitFlag65536 = 0x10000
	};
	const unsigned int standartByteSize = 8;//assuming 8 bit 
};

template <typename _Ty>
void printBits(const _Ty& bitInfo){//from 1 to 16 bit , iam to lazy for all 32 or 64 ;)

	using namespace GlobalEnums;
	using namespace std;
	cout << "current setting   : ";
	for (int i = sizeof(bitInfo) * standartByteSize; i > 0; i--){//checking bit´s
		switch (i){

		case 1: if (bitInfo & bitFlag1) cout << "1";
				else cout << "0";
				break;
		case 2: if (bitInfo & bitFlag2) cout << "1";
				else cout << "0";
				break;
		case 3: if (bitInfo & bitFlag4) cout << "1";
				else cout << "0";
				break;
		case 4: if (bitInfo & bitFlag8) cout << "1";
				else cout << "0";
				break;
		case 5: if (bitInfo & bitFlag16) cout << "1";
				else cout << "0";
				break;
		case 6: if (bitInfo & bitFlag32) cout << "1";
				else cout << "0";
				break;
		case 7: if (bitInfo & bitFlag64) cout << "1";
				else cout << "0";
				break;
		case 8: if (bitInfo & bitFlag128) cout << "1";
				else cout << "0";
				break;
		case 9: if (bitInfo & bitFlag256) cout << "1";
				else cout << "0";
				break;
		case 10: if (bitInfo & bitFlag512) cout << "1";
				 else cout << "0";
				 break;
		case 11: if (bitInfo & bitFlag1024) cout << "1";
				 else cout << "0";
				 break;
		case 12: if (bitInfo & bitFlag2048) cout << "1";
				 else cout << "0";
				 break;
		case 13: if (bitInfo & bitFlag4096) cout << "1";
				 else cout << "0";
				 break;
		case 14: if (bitInfo & bitFlag8192) cout << "1";
				 else cout << "0";
				 break;
		case 15: if (bitInfo & bitFlag16384) cout << "1";
				 else cout << "0";
				 break;
		case 16: if (bitInfo & bitFlag32768) cout << "1";
				 else cout << "0";
				 break;
		default:
			break;
		}
		if (i == 9) cout << " ";
	}
}


template <typename _Ty,bool showInfo=false>
void printInfo(const _Ty& bitInfo){//from 1 to 16 bit , to lazy for all 32 or 64 ;)

	using namespace GlobalEnums;
	using namespace std;
	if (showInfo){
		for (int i = 0; i < sizeof(bitInfo) * standartByteSize; i++){//checks if bitInfo bit(i) is set to 1
			switch (i + 1){
			case 1: if (bitInfo & bitFlag1) cout << "bitFlag 1 set" << endl;
				break;
			case 2: if (bitInfo & bitFlag2) cout << "bitFlag 2 set" << endl;
				break;
			case 3: if (bitInfo & bitFlag4) cout << "bitFlag 4 set" << endl;
				break;
			case 4: if (bitInfo & bitFlag8) cout << "bitFlag 8 set" << endl;
				break;
			case 5: if (bitInfo & bitFlag16) cout << "bitFlag 16 set" << endl;
				break;
			case 6: if (bitInfo & bitFlag32) cout << "bitFlag 32 set" << endl;
				break;
			case 7: if (bitInfo & bitFlag64) cout << "bitFlag 64 set" << endl;
				break;
			case 8: if (bitInfo & bitFlag128) cout << "bitFlag 128 set" << endl;
				break;
			case 9: if (bitInfo & bitFlag256) cout << "bitFlag 256 set" << endl;
				break;
			case 10: if (bitInfo & bitFlag512) cout << "bitFlag 512 set" << endl;
				break;
			case 11: if (bitInfo & bitFlag1024) cout << "bitFlag 1024 set" << endl;
				break;
			case 12: if (bitInfo & bitFlag2048) cout << "bitFlag 2048 set" << endl;
				break;
			case 13: if (bitInfo & bitFlag4096) cout << "bitFlag 4096 set" << endl;
				break;
			case 14: if (bitInfo & bitFlag8192) cout << "bitFlag 8192 set" << endl;
				break;
			case 15: if (bitInfo & bitFlag16384) cout << "bitFlag 16384 set" << endl;
				break;
			case 16: if (bitInfo & bitFlag32768) cout << "bitFlag 32768 set" << endl;
				break;

			default:
				break;
			}
		}
	}
	if (sizeof(bitInfo) * standartByteSize == sizeof(unsigned short) * standartByteSize){
		cout << "bitInfo short sum : " << static_cast<int>(bitInfo) << endl;
		printBits<decltype(bitInfo)>(bitInfo);
		cout << "\n" << endl;
	}

	if (sizeof(bitInfo) * standartByteSize == sizeof(unsigned char) * standartByteSize){
		cout << "bitInfo char  sum : " << static_cast<int>(bitInfo) << endl;
		printBits<decltype(bitInfo)>(bitInfo);
		cout << "\n" << endl;
	}

}


int main(){

	using namespace std;
	using namespace::GlobalEnums;

	unsigned char test = 211;  //unsigned char 8 bit Range 0 to 255
	unsigned short test2 = 32767;//unsigned short 16 bit Range 0 to 65535

	//print Infos -> printInfo<type,true> displays additional infos
	printInfo<unsigned char>(test);
	printInfo<unsigned short>(test2);
	cout << "press Enter"<<endl;
	cin.get();
	
	//resetting 
	test = 0;
	test2 = 0;

	//setting test to 255
	test |= bitFlag128;//using |= to set bit in Position(8) to true
	test |= bitFlag64;
	test |= bitFlag32;
	test |= bitFlag16;
	test |= bitFlag8;
	test |= bitFlag4;
	test |= bitFlag2;
	test |= bitFlag1;

	//setting test2 to 17456
	test2 |= bitFlag16384;
	test2 |= bitFlag1024;
	test2 |= bitFlag16;
	test2 |= bitFlag32;

	printInfo<unsigned char, true>(test);
	printInfo<unsigned short, true>(test2);

	cout << "press Enter to quit";
	cin.get();

}
 
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods