Below is a program in which I am trying to reset a particular bit of a hexadecimal number. The bit location, number of bits to reset, and hexadecimal value all are user inputs.
Header file
#pragma once
int bit0(int i,unsigned int RegA,unsigned int RegB,int s[]);
C file
#include "stdafx.h"
#include "stdio.h"
#include "conio.h"
#include "iostream"
#include "targetver.h"
#include "bit.h"
int bit0(int i,unsigned int RegA,unsigned int RegB,int s[])
{
unsigned int j=0;
unsigned int K=0;
unsigned int L=0;
printf("\nThe bit is at s[0] is %x\n", s[0]);
for (j=0; j<i; j++)
{
K = (1 << s[j]);
L = ~K;
RegB = RegA & ~L;
printf("\nThe bit is %x\n", RegB);
if (RegB | 0)
{
RegB = RegB & ~ (1 << s[j]);
}
else
{
RegB;
}
}
printf("\nThe new reset bit is %x\n", RegB);
_getch();
return 0;
}
main file
#include "stdafx.h"
#include "stdio.h"
#include "conio.h"
#include "iostream"
#include "targetver.h"
#include "bit.h"
int main()
{
int i=0;
int j=0;
int s[35]={0};
unsigned int RegA = 0;
unsigned int RegB = 0;
printf("Enter the hexa decimal value to be reset ");
scanf_s("%x", &RegA);
printf("Entered hexa decimal value is %x ", RegA);
printf("\nHow many decimal places needs to be reset (0-31) ?");
scanf_s("%d", &i);
printf("Enter the decimal places that needs to be reset ");
for (j=0; j<i; j++)
{
scanf_s("%d", &s[j]);
}
///// check the entered hex value on those decimals places as bit 0 or bit 1
bit0(i,RegA,RegB,s);
_getch();
return 0;
}
I am compiling and running executing the above code using Visual Studio.
The problem is in the C file, on the RegB = RegA & ~L;
line. The AND operation seems not to be taking place because I am getting 0 as the RegB
value.
Program input:
Enter the hexadecimal value to be reset : 0100 1111
Entered hexadecimal value is : 0100 1111
How many decimal places needs to be reset (0-31): 1
Enter the decimal places that needs to be reset : 1