Click here to Skip to main content
15,911,039 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
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

C#
#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);

}
Posted
Updated 22-Aug-12 6:53am
v2
Comments
Sergey Alexandrovich Kryukov 22-Aug-12 13:02pm    
Why do you think this is unexpected? What's the value of data on input? That's all about it, nothing else.
--SA
Legor 23-Aug-12 3:17am    
Please accept one of the below answers.

In your code, you set a bit correctly. If your original value of data was 0, you would get your 8 if the bit index n 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.

[EDIT]

I just noted that you shift data, not 1. This is a problem. Your value of p is irrelevant, because it's 0; ORing it does not change anything. You print shifted data instead of shifting a bit.

You would need:
C#
int setbit(unsigned char data, int shift) {
   return data | (1 << shift);
}


[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.

—SA
 
Share this answer
 
v5
Comments
steven8Gerrard 22-Aug-12 13:13pm    
No . The value of data is 1 which is 0001 in binary and bit that needs to be set is 3rd bit if it sets it value should be 1000 (i.e) 8 by the logic i have written no?
Sergey Alexandrovich Kryukov 22-Aug-12 13:20pm    
No! Please see the update to my answer, after [EDIT].
--SA
Sergey Alexandrovich Kryukov 22-Aug-12 13:23pm    
Sorry, no. I did not pay attention that you don't use data (it's totally ignored, you use p and initialize to 0); so setbit(1, 3) prints 8 as expected. I even decided to compile and test it just in case. Check it up.
--SA
Sergey Alexandrovich Kryukov 22-Aug-12 13:30pm    
By the way, what's on input, '1' or 1 (binary) that is, '\0x01'? If '1', see the answer by Richard: '1' is actually 0b10001 (I did not check it, assuming he calculated correctly).
--SA
Your setbit doesn't actually set any bits, it just shifts data by n bit positions and then prints it, returning nothing (as Sergey noted).

To actually set the specified bit in data you need something like:
C++
unsigned char setbit(unsigned char data, int n)
{
    data |= 1 << n;
    printf("\n%d",data);
    return data;
}

This will return (and print) data with the n-th bit (from the low-order end) set.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 22-Aug-12 13:35pm    
Voted 5. Please see my answer: you probably saw it when I did a mistake because I did not notice that data was shifted (I often assume that some people do more reasonable things than they actually do), so I scratched it all and fixed.
Also, how Richard noted, it could be '1', not binary 1 ('0x01'), which could get what OP did not expect. -- Please see my answer.
--SA
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:
C++
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);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900