下面是一个程序,我试图在其中重置十六进制数的特定位。位位置、要重置的位数和十六进制值都是用户输入。
头文件
#pragma once
int bit0(int i,unsigned int RegA,unsigned int RegB,int s[]);
C 文件
#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;
}
主文件
#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;
}
我正在使用 Visual Studio 编译并运行执行上述代码。
问题出在C文件中,就RegB = RegA & ~L;
行了。AND 操作似乎没有发生,因为我得到 0 作为RegB
值。
程序输入:
输入要重置的十六进制值:0100 1111
输入的十六进制值为:0100 1111
需要重置多少位小数(0-31):1
输入需要重置的小数位:1