Giter VIP home page Giter VIP logo

bitwise-operation's Introduction

Bitwise-Operation

In C, the following 6 operators are bitwise operators (also known as bit operators as they work at the bit-level). They are used to perform bitwise operations in C.

  • 1.The & (bitwise AND) in C or C++ takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.
  • 2.The | (bitwise OR) in C or C++ takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if any of the two bits is 1.
  • 3.The ^ (bitwise XOR) in C or C++ takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.
  • 4.The << (left shift) in C or C++ takes two numbers, the left shifts the bits of the first operand, and the second operand decides the number of places to shift.
  • 5.The >> (right shift) in C or C++ takes two numbers, right shifts the bits of the first operand, and the second operand decides the number of places to shift.
  • 6.The __~ (bitwise NOT)__ in C or C++ takes one number and inverts all bits of it.

bitwose-ope


Code I

//Bitwise_operation
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main() {
		//To count the number of 1's
	int num,count=0;
	printf("Enter the number:\n");
	scanf("%d",&num);
	
	do{
		if(num&1){
		count++;
		}
		num=num>>1;
		
	}while(num!=0);
	printf("Number of 1's is:%d",count);
	//To check number is even or odd
	if(num&0){
		printf("Number is odd");
	}
	else {
		printf("Number is even");
	}
	
	return 0;
}

Output I

Screenshot 2023-04-11 154745

Code II

//Bitwise_main
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main() {
	int x,y,z;
	char bitwise;
	printf("Enter the Bitwise operator:");
	scanf("%c",&bitwise);
	printf("Enter the first number:\n");
	scanf("%d",&x);
	printf("Enter the second number:\n");
	scanf("%d",&y);
	


	
	switch (bitwise)
	{
		case '&':
			z=x&y;
			printf("Bitwise AND of x & y is:%d\n",z);
			break;
		case '|':
			z=x|y;
			printf("Bitwise OR of x & y is:%d\n",z);
			break;
		case '^':
			z=x^y;
			printf("Bitwise XOR of x & y is:%d\n",z);
			break;
		case '~':
			z=x~y;
			printf("Bitwise NOT of x & y is:%d\n",z);
			break;
		default :
			printf("Enter a Valid Bitwise Operator(&/|/^/~)");
	}
	
	
	return 0;
}

Output

Screenshot 2023-04-11 154314

bitwise-operation's People

Contributors

abhishek-singh296 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.