Thread: Bitwise operators help ~

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    67

    Bitwise operators help ~

    First please view my code below.

    Code:
    #include <stdio.h>
    #include <conio.h>
    
        void ReduceOddNumber(int n, int *result);
    
        int main(void)
        {
            int n, result;
    
            printf("Input integer: ");
            scanf("%d", &n);
    
            if( n & 1 == 1) // if last bit in numbers is 1, 'n' is odd , 0 for even
            {
                printf("Odd number.\n");
    
                ReduceOddNumber(n, &result);
            }
    
            else
            {
                printf("Even number.\n");
            }
        
        }
    
        void ReduceOddNumber(int n, int *result)
        {
            result = 
        }
    I was newly learn Bitwise operators and now I have no ideas how to start with a simple thing. How can I reduce number by 1 by Bitwise operators? E.g. User input 15 then my program change to 14? Very thanks you.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You want to clear the bit that causes n&1 to give a non-zero result, but leave all the other bits of n untouched.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    67
    Did you mind show me how to done that? Because I really have no idea how...I am totally new at this chapter.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If I gave you the clearest answer it would be like solving the problem.

    You need to know what the operators do to get anywhere.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    67
    Ok...I try change into the following code

    Code:
        void ReduceOddNumber(int n, int *result)
        {
            *result = n << 1 ;
        }
    When I input 5 it become 10 I wonder why?

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    67
    Now I get it ~ If I change into *result = n ^ 1 ; It works....But I still don't know how those operators works. So I need some explanation...

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I recommend reading the FAQ on this topic, it should explain all the operators to you. Once you understand what they do, you just have to glue them together to do what you want. What you discovered happens to be one of the simplest answers because it involves the fewest operations. Basically because 1 XOR 1 == 0, you get what you want because all odd numbers on your system will have the one bit set. There are better ways to do what you want, but I'll leave it to you to discover if you care (it's not that hard!)

  8. #8

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bitwise operators
    By manzoor in forum C++ Programming
    Replies: 12
    Last Post: 08-18-2008, 02:43 PM
  2. Bitwise operators. What's this????????
    By money? in forum C Programming
    Replies: 20
    Last Post: 06-17-2003, 06:03 PM
  3. Bitwise Operators....
    By o0obruceleeo0o in forum C++ Programming
    Replies: 21
    Last Post: 04-09-2003, 06:45 AM
  4. bitwise operators
    By Qasim in forum C Programming
    Replies: 1
    Last Post: 08-31-2001, 09:23 AM