Thread: bit manipulation need help urgent

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    222

    Thumbs up bit manipulation need help urgent

    Code:
    please help me with the following c code
    
    Bit manipulation: 
    a. In the following code snippet: 
    i. Replace xxxx with code that sets bit 3 of variable a. 
    ii. Replace zzzzz with code that sets a equal to b with bit 4 cleared 
    iii. Replace mmm with code that sets a equal to b with bit 5 set and bit 7 cleared 
    
    int myfunc(int b) 
    { 
    int a = 0; 
    if (b == 10) 
    xxxx // set bit 3 of a 
    else if (b == 20) 
    zzzzzz // a = b with bit 4 cleared 
    else 
    mmmmm // a = b with bit 5 set and 7 cleared 
    return a; 
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Read the homework policy.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Then read the bitwise operator FAQ.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    This may help you out: Bitwise Operators in C

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    222
    Code:
    Hi Syscal
    
    
    I have some doubts here.
    
    
    To set bit3 of variable a, 
    I used this way
    
    void set_bit3(void) // set bit 3 of a 
    {
    a |= BIT3;
    }
    
    
    Is it correcT?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How about you write a small test program to find out? One that actually fully compiles and runs.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bit manipulation
    By roboscv in forum C Programming
    Replies: 4
    Last Post: 02-09-2012, 09:05 AM
  2. bit manipulation
    By rrlangly in forum C Programming
    Replies: 5
    Last Post: 07-18-2011, 07:30 AM
  3. bit manipulation in C#
    By luigi40 in forum C# Programming
    Replies: 4
    Last Post: 06-21-2005, 01:15 AM
  4. Bit manipulation
    By pkalluri in forum C++ Programming
    Replies: 5
    Last Post: 05-12-2003, 07:06 AM