Your code is not working on bits as you assume, but characters. If the value of
dat
is 1 then it contains the
character 1 which is
00110001
in binary. Shifting that left 3 bits gives you
00110001000
and taking only the low order 8 bits, since you are storing it in a character variable, you have
10001000
which equals 136. Change your code to the following to resolve this:
void main()
{
int m;
int dat;
scanf("\n%d", &dat); scanf("\n%d",&m); printf("\n");
setbit(dat,m);
}
void setbit(unsigned char data, int n)
{
int p;
p = data << n;
printf("\n%d",p);
}