0% found this document useful (0 votes)
22 views

Page - 1

The document contains code snippets and explanations for various operations using bitwise operators in C programming. It includes programs to check status of a bit, set/clear/toggle a bit, determine if a number is even/odd, positive/negative, power of 2, divisible by 8. Other programs rotate bits, convert case, reverse bits, compare numbers, print float binary, swap adjacent bytes of a hex number, delete bits from a position, swap nibbles. The document provides code examples for these common bit manipulation tasks.

Uploaded by

R vignesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Page - 1

The document contains code snippets and explanations for various operations using bitwise operators in C programming. It includes programs to check status of a bit, set/clear/toggle a bit, determine if a number is even/odd, positive/negative, power of 2, divisible by 8. Other programs rotate bits, convert case, reverse bits, compare numbers, print float binary, swap adjacent bytes of a hex number, delete bits from a position, swap nibbles. The document provides code examples for these common bit manipulation tasks.

Uploaded by

R vignesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Table of Contents

WAP to check status of a given bit of a Number. ....................................................... 2

Write a program for the following one......................................................................... 3

WAP to find the given number is even or odd using bitwise operators. ...................... 5

WAP to find the given number is +ve or -ve using bitwise operators.......................... 6

WAP to swap two numbers using bitwise operators. .................................................. 7

WAP to find the given number is power of 2 or not. ................................................... 8

WAP to find the given number is divisible by 8 or not using bitwise operators. .......... 9

Write a program to rotate the bits. Input the no of rotations, at runtime. ................ 10

Convert the characters Upper to Lower & Lower to Upper using bitwise operators. 11

Write a program to reverse the bits of a given number............................................. 12

Write a one line code to compare two numbers using bitwise operators. ................. 14

Write a program to print float binary formation using char *ptr. ................................ 15

Write a program to swap the adjacent bytes of a given 4-digit hex number. ............ 17

Write a program to delete no of bits from particular position in a given number. ...... 19

Write a macro for swapping first and last nibbles in a given integer. ........................ 19

Write a logic to extract P bits from Posion N in an integer M ................................... 19

Write a macro to clear a bit at the position N in an integer M. .................................. 19

There are 48 bits are stored in an array of character buffer and store them into 2

integer variables. ...................................................................................................... 19

Page | 1
Bitwise Operators

WAP to check status of a given bit of a Number.

#include<stdio.h>

main()

int num,pos,r;

printf("Enter number and pos\n");

scanf("%d %d",&num,&pos);

(num >> pos & 1) ? printf("Set\n"):printf("Clear\n");

//(num & 1 << pos) > 0 printf("Set\n"):printf("Clear\n");

Page | 2
Write a program for the following one.

a) Set a bit b) Clear a bit c) Toggle a bit

#include<stdio.h>

main()

int num,pos,r;

char op;

printf("Enter Number and position\n");

scanf("%d %d",&num,&pos);

printf("\nEnter\n1) 's' to set\n2) 'r' to reset\n3) 'c' to complement\n");

printf("Enter Choice\n");

scanf(" %c",&op);

switch(op)

case 's':

num = num | 1 << pos;

printf("\nAnswer:%d\n",num);

break;

case 'r':

Page | 3
num = num & ~(1 << pos);

printf("\nAnswer:%d\n",num);

break;

case 'c':

num = num ^ (1 << pos);

printf("\nAnswer:%d\n",num);

break;

default:

printf("\nInvalid Chice\n");

Page | 4
WAP to find the given number is even or odd using bitwise operators.

#include<stdio.h>

main()

int num;

printf("Enter Number:\n");

scanf("%d",&num);

printf("By AND logic\n");

(num & 1)?printf("Odd\n"):printf("Even\n");

printf("By Modulus logic\n");

(num % 2)?printf("Odd\n"):printf("Even\n");

printf("By Divide and Multiplication\n");

((num/2) * 2 == num) ? printf("Even\n"):printf("Odd\n");;

Page | 5
WAP to find the given number is +ve or -ve using bitwise operators.

#include<stdio.h>

main()

int num;

printf("Enter Number\n");

scanf("%d",&num);

printf("By Relational Operator\n");

if(num < 0)

printf("Negative Number\n");

else

printf("Positive Number");

printf("By AND Logic\n");

(num & 1 << sizeof(int)*8-1) ? printf("Negative


Number\n"):printf("Positive Number\n");

Page | 6
WAP to swap two numbers using bitwise operators.

#include<stdio.h>

main()

int num1,num2;

printf("Enter two number\n");

scanf("%d %d",&num1,&num2);

printf("\nBEFORE : %d \t%d\n",num1,num2);

num1 = num1 ^ num2;

num2 = num1 ^ num2;

num1 = num1 ^ num2;

//num2 = num1+num2 - (num1=num2);

//num2 = num1*num2 / (num1=num2);

printf("AFTER : %d \t%d\n",num1,num2);

Page | 7
WAP to find the given number is power of 2 or not.

#include<stdio.h>

main()

int num;

printf("Enter Number\n");

scanf("%d",&num);

(num & num - 1)? printf("Not\n"):printf("Power of two\n");

Page | 8
WAP to find the given number is divisible by 8 or not using bitwise
operators.

#include<stdio.h>

main()

int num;

printf("Enter Number\n");

scanf("%d",&num);

num & 7 ? printf("No\n"):printf("Yes Divisible by 8\n");

Page | 9
Write a program to rotate the bits. Input the no of rotations, at
runtime.
Ex : binary : 10000000000000000000000000001011

rotations : suppose 3 times right, then

result : 01110000000000000000000000000001

binary : 10000000000000000000000000001011

rotations : suppose 4 times left, then

result : 00000000000000000000000010111000

Page | 10
Convert the characters Upper to Lower & Lower to Upper using
bitwise operators.

#include<stdio.h>

main()

char n;

printf("Enter Character\n");

scanf("%c",&n);

n = n ^ 32;

//n = n ^ (1 << 5);

//n = n ^ ' ';

printf("Converted Case is %c\n",n);

Page | 11
Write a program to reverse the bits of a given number.

#include<stdio.h>

main()

int num,i,j;

printf("Enter Number\n");

scanf("%d",&num);

for(i=sizeof(int)*8-1;i>=0;i--)

printf("%d",num >> i & 1);

if (i % 8 == 0)

printf(" ");

printf("\n");

for(i=0,j=sizeof(int)*8-1;i<(sizeof(int)*8)/2;i++,j--)

if( (num >> i & 1) != (num >> j & 1) )

Page | 12
{

num = num ^ 1 << i;

num = num ^ 1 << j;

for(i=sizeof(int)*8-1;i>=0;i--)

printf("%d",num >> i & 1);

if (i % 8 == 0)

printf(" ");

printf("\n");

Page | 13
Write a one line code to compare two numbers using bitwise
operators.

#include<stdio.h>

main()

int n1,n2;

printf("Enter Two Values:\n");

scanf("%d %d",&n1,&n2);

n1 ^ n2 ? printf("Different\n"):printf("Same\n");

Page | 14
Write a program to print float binary formation using char *ptr.

#include<stdio.h>

main()

float num;

char *c=&num;

int i,j;

printf("Enter the Number : \n");

scanf("%f",&num);

c=c+3;

for(i=0;i<4;i++)

for(j=7;j>=0;j--)

printf("%d",(*c) >> j & 1);

printf(" ");

c=c-1;

printf("\n");

Page | 15
Page | 16
Write a program to swap the adjacent bytes of a given 4-digit hex
number.

Ex : given number = 0x1234;

after swap : 0x3412;

#include<stdio.h>

main()

int i,j,k,num,m,n;

printf("Enter Number\n");

scanf("%d",&num);

printf("%x\n",num);

for(i=sizeof(int)*8-1;i>=0;i--)

printf("%d",num >> i & 1);

if(i % 8 == 0)

printf(" ");

printf("\n");

for(i=0,j=8;i<=7;i++,j++)

if( (num >> i & 1) != (num >> j & 1))

Page | 17
{

num = num ^ 1 << i;

num = num ^ 1 << j;

//num = (num & 0xff) << 8 | (num & 0xff00) >> 8;

for(i=sizeof(int)*8-1;i>=0;i--)

printf("%d",num >> i & 1);

if(i % 8 == 0)

printf(" ");

printf("\n%x",num);

printf("\n");

Page | 18
Write a program to delete no of bits from particular position in a
given number.

Input the no of bits, at runtime.

Ex: Suppose num = 100;

It's Binaray is 00000000000000000000000001100100

delete 2 bits from 4th position

then result is 00000000000000000000000000011100

Write a macro for swapping first and last nibbles in a given integer.

Ex: Suppose num = 10

It's Binary is 0000000000000000000000000001010

After swap 1010000000000000000000000000000

Write a logic to extract P bits from Posion N in an integer M

Write a macro to clear a bit at the position N in an integer M.

There are 48 bits are stored in an array of character buffer and store
them into 2 integer variables.

Page | 19

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy