How to Set / Clear and Toggle a single bit in C/C++

To Set / Clear and Toggle a single bit in C++ see the methods below.

Setting a bit

Use the bitwise OR operator (|) to set a bit.

number |= 1 << x;

Clearing a bit

Use the bitwise AND operator (&) to clear a bit.

number &= ~(1 << x);

Toggling a bit

The XOR operator (^) can be used to toggle a bit.

number ^= 1 << x;

Source

Please rate this snippet

How to check if a file exists in C++

To check if a file exists in C++ you can use the following snippet.

Sample C++

#include <sys/stat.h>

inline bool fileExists (const std::string& fileName) {
	struct stat buff;   
	return (stat (fileName.c_str(), &buff) == 0); 
}

Please rate this snippet