15,747,637 members
See more:
Description:

Read a number, M and N from the user. You need to check whether N th bit is set(1) or not, If yes, then you need to clear the M th bit of the number and print the updated value of num

Pre-requisites:

Bitwise operators

Sample Execution:

Test Case 1:

Enter the number: 19

Enter 'N': 1

Enter 'M': 4

Updated value of num is 3

Test Case 2:

Enter the number: 19

Enter 'N': 2

Enter 'M': 4

Updated value of num is 19

What I have tried:

C++
```#include<stdio.h>
int main()
{
int num,N,M,K;
printf("Enter a number:");
scanf("%d",&num);
printf("Enter 'N':");
scanf("%d", &N);
num=num>>(N-1);
if((num&1)!=0)
{
printf("Enter 'M': ");
scanf("%d", &M);

K = num & ~(1 << M);
printf("Updated value of num is %d \n", K);
}
return 0;
}```
Posted
Updated 12-Jan-23 13:23pm
v3

## Solution 2

First off, that's not C#, it's C - they may look similar, but they are very, very different languages! Tagging your questions with the wrong languages can mean you wait a lot longer for a reply ...

Second, indent your code! It makes it a whole lot easier to read:
C
```#include<stdio.h>
int main()
{
int num,N,M,K;
printf("Enter a number:");
scanf("%d",&num);
printf("Enter 'N':");
scanf("%d", &N);
num=num>>(N-1);
if((num&1)!=0)
{
printf("Enter 'M': ");
scanf("%d", &M);

K = num & ~(1 << M);
printf("Updated value of num is %d \n", K);
}
return 0;
}```
It's less relevant with trivial fragmente like this, but get into the habit early and it can save you hours of frustration later.

Third, when you post a question, you need to explain what problem you have met: we have no idea what values you entered, or what happened when you did - so we have to guess and that may not address the problem you have found at all!
Start here: Asking questions is a skill[^] and think about what you need to know, and what you need to tell us in order to get help.

Do note that when you try to check if the Nth bit of `num` is set, you overwrite the existing value - which means you cannot complete the task as you don't have the original value entered by the user any more!
Instead of shifting `num` at all, I'd shift `1` N places to the left and AND that with num to check if it is set.

CPallini 12-Jan-23 6:37am
5.
Raju Kumar 2023 12-Jan-23 6:53am
OriginalGriff 12-Jan-23 7:26am
Where? I see no changes...

## Solution 1

I'll point you in the right direction.