C++ bitwise operators

Created By: chatGPT

Bitwise operators in C++ are used to perform operations on individual bits of integer types. They allow for manipulation of the binary representation of numbers. Here is a brief overview of the common bitwise operators:
1. AND (&): This operator compares each bit of two integers. If both bits are 1, the resulting bit is set to 1; otherwise, it is set to 0.
int a = 5; // binary: 0101
int b = 3; // binary: 0011
int result = a & b; // result: 1 (binary: 0001)
2. OR (|): This operator sets each bit to 1 if at least one of the bits is 1.
int a = 5; // binary: 0101
int b = 3; // binary: 0011
int result = a | b; // result: 7 (binary: 0111)
3. XOR (^): This operator sets each bit to 1 if only one of the bits is 1.
int a = 5; // binary: 0101
int b = 3; // binary: 0011
int result = a ^ b; // result: 6 (binary: 0110)
4. NOT (~): This operator inverts the bits of an integer; if the bit is 1, it becomes 0, and vice versa.
int a = 5; // binary: 0101
int result = ~a; // result: -6 (binary: 1010 in 2's complement)
5. Left Shift (<<): This operator shifts the bits of the first operand to the left by the number of positions specified by the second operand. Each shift to the left effectively multiplies the number by 2.
int a = 5; // binary: 0101
int result = a << 1; // result: 10 (binary: 1010)
6. Right Shift (>>): This operator shifts the bits of the first operand to the right by the number of positions specified by the second operand. Each shift to the right effectively divides the number by 2.
int a = 5; // binary: 0101
int result = a >> 1; // result: 2 (binary: 0010)
Example Usage: Below is a small program demonstrating the use of various bitwise operators in C++.
#include <iostream>

int main() {
    int a = 5; // 0101
    int b = 3; // 0011

    std::cout << "AND: " << (a & b) << std::endl; // 1
    std::cout << "OR: " << (a | b) << std::endl; // 7
    std::cout << "XOR: " << (a ^ b) << std::endl; // 6
    std::cout << "NOT: " << (~a) << std::endl; // -6
    std::cout << "Left Shift: " << (a << 1) << std::endl; // 10
    std::cout << "Right Shift: " << (a >> 1) << std::endl; // 2
    return 0;
}
Introduction And SetupVariablesData TypesIntFloatDoubleCharBoolStringVoidConstantsOperatorsArithmetic OperatorsRelational OperatorsLogical OperatorsBitwise OperatorsAssignment OperatorsTernary OperatorControl FlowIf StatementElse StatementElse If StatementSwitch StatementFor LoopWhile LoopDo While LoopBreak StatementContinue StatementGoto StatementFunctionsFunction DeclarationFunction DefinitionFunction OverloadingInline FunctionsDefault ArgumentsRecursionPass By ValuePass By ReferenceData StructuresArraysMultidimensional ArraysStructsEnumsUnionsClassesObjectsMember FunctionsConstructorsDestructorsAccess SpecifiersPublicPrivateProtectedInheritancePolymorphismFunction OverridingVirtual FunctionsPure Virtual FunctionsOperator OverloadingTemplatesNamespaceCommentsSingle Line CommentsMulti Line CommentsSyntaxSemicolonCurly BracesParenthesesEvent HandlingCinCoutError HandlingTry BlockCatch BlockThrow StatementException HandlingPreprocessor DirectivesInclude GuardsMacro DefinitionsInline AssemblyStatic KeywordsExtern KeywordsConst KeywordsVolatile KeywordsFriend FunctionStatic MemberThis PointerDynamic Memory AllocationNew OperatorDelete Operator