在c中设置位 [英] Setting the bit in c
本文介绍了在c中设置位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我程序的目的是当用户提供数据和位时.数据中的特定位应置位.我曾在Visual Studio中尝试过该程序,但输出错误.所以我希望有人可以帮助我
这是我的程序
Aim of my program is when user gives the data and bit . The particular bit in data should be set . I have tried this program in visual studio but i''m getting a wrong output . So i hope somebody could help me
Here is my program
#include "stdafx.h"
//#define p 0x00;
void setbit (unsigned char data_ , int n_);
void main()
{
int m;
char dat;
scanf("\n%c", &dat);// Receive data value from user
scanf("\n%d",&m);// Receive the bit which should be set
printf("\n");
setbit(dat,m);
}
void setbit(unsigned char data, int n)
{
unsigned char p=0;
p = p|(data<<n); // For ex if data is 1 and third bit should be set in that data 1 equals to 0001 and third bit should be set . Shifting it thrice makes it 1000 (i.e) 8 But i get 136
printf("\n%d",p);
}
推荐答案
在您的代码中,您进行了正确设置.如果data
的原始值为0,则如果位索引n
为3,则将得到8,都正确.但是,如果初始值有所不同,则可以对第三个设置位进行或"运算,如果该位已经设置,则将获得与或"运算前相同的值,否则将其加8.
我只是注意到您移动了data
,而不是1.这是一个问题.您的p
值无关紧要,因为它的值为0.对其进行或运算不会更改任何内容.您打印移位的data
而不是移位.
您需要:
In your code, you set a bit correctly. If your original value ofdata
was 0, you would get your 8 if the bit indexn
is 3, all correct. But if the initial value is something different, you OR the third set bit to it, and, if that bit is already set, you get the same value as before ORing, or add 8 otherwise.
I just noted that you shiftdata
, not 1. This is a problem. Your value ofp
is irrelevant, because it''s 0; ORing it does not change anything. You print shifteddata
instead of shifting a bit.
You would need:
int setbit(unsigned char data, int shift) {
return data | (1 << shift);
}
[END EDIT]
顺便说一句,我希望您理解您的setbit
函数不会进行任何修改,因为您按值传递值,而仅打印新值.只是检查.
[END EDIT]
By the way, I hope you understand that your setbit
function does not modify anything because you pass the values by value, it only prints the new value. Just checking.
您的setbit
实际上没有设置任何位,只是将data
移位了n
位,然后打印出来,不返回任何内容(如Sergey所述).
要实际上设置data
中的指定位,您需要类似以下内容:
Yoursetbit
doesn''t actually set any bits, it just shiftsdata
byn
bit positions and then prints it, returning nothing (as Sergey noted).
To actually set the specified bit indata
you need something like:
unsigned char setbit(unsigned char data, int n)
{
data |= 1 << n;
printf("\n%d",data);
return data;
}
这将返回(并打印)设置了第n
位(从低端开始)的data
.
This will return (and print) data
with the n
-th bit (from the low-order end) set.
您的代码无法像您假定的那样在位上工作,但是字符.如果dat
的值为1,则它包含字符 1,它是二进制形式的00110001
.向左移动3位将为您提供00110001000
,而仅接受低8位,因为您将其存储在字符变量中,因此具有等于136的10001000
.将您的代码更改为以下代码即可解决此问题:
Your code is not working on bits as you assume, but characters. If the value ofdat
is 1 then it contains the character 1 which is00110001
in binary. Shifting that left 3 bits gives you00110001000
and taking only the low order 8 bits, since you are storing it in a character variable, you have10001000
which equals 136. Change your code to the following to resolve this:
void main()
{
int m;
int dat;
scanf("\n%d", &dat);// Receive bit value from user
scanf("\n%d",&m);// Receive the bit which should be set
printf("\n");
setbit(dat,m);
}
void setbit(unsigned char data, int n)
{
int p;
p = data << n;
printf("\n%d",p);
}
这篇关于在c中设置位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文