bitwise operators

This is a discussion on bitwise operators within the C Programming forums, part of the General Programming Boards category; hello! I want to know how you use the bitwise operators ( &, |,^, <<, >>, ~) A sample program ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    2

    bitwise operators

    hello!


    I want to know how you use the bitwise operators ( &, |,^, <<, >>, ~) A sample program of how they work would be helpful.
    thank you!



  2. #2
    Registered User *pointer's Avatar
    Join Date
    Oct 2001
    Posts
    74
    Code:
    int count_ones( unsigned x ) {
        int ones;
    
        /* Loop as long as x has 1 bits in it */
        for( ones = 0; x != 0; x >>= 1 ) {
            /* If the lowest order bit is 1, count it */
            if( ( x & 1 ) != 0 ) {
                ones += 1;
            }
        }
    
        return ones;
    }
    This is a simple program that counts the ones in a binary number.
    pointer = NULL

  3. #3
    Registered User Natase's Avatar
    Join Date
    Aug 2001
    Posts
    123
    I know how these work but my explanation will probably be terrible...

    As you know(?) numbers are stored as bits (1's and 0's)

    e.g. 10101010 == 170

    >> and << shifts this number to the right or left respectively

    10101010 >> 1 (shift 1 place to the right) == 01010101

    10101010 << 1 == 01010100

    In both cases you lose the last digit and place a zero in the first digit.

    ~ reverses the number

    ~10101010 == 01010101
    ~11111111 == 00000000

    & | and ^ use the following tables:

    Code:
    & : 1 : 0      | : 1 : 0     ^ : 1 : 0
    1 : 1 : 0      1 : 1 : 1     1 : 0 : 1
    0 : 0 : 0      0 : 1 : 0     0 : 1 : 0
    had to edit this... looked really messy...

    operator: bit == 1 : bit == 0
    bit ==1 : }
    bit == 0 : } results of operations

    Someone correct me if I'm wrong...
    Last edited by Natase; 10-05-2001 at 11:09 AM.

  4. #4
    Registered User *pointer's Avatar
    Join Date
    Oct 2001
    Posts
    74
    Looks okay to me except for your operator tables
    & | and ^ use the following tables:


    code:--------------------------------------------------------------------------------
    & : 1 : 0 | : 1 : 0 ^ : 1 : 0
    1 : 1 : 0 1 : 1 : 1 1 : 0 : 1
    0 : 0 : 0 0 : 1 : 0 0 : 1 : 0
    --------------------------------------------------------------------------------
    Here's the correct table
    Code:
    & : 0 : 1    | : 0 : 1    ^ : 0 : 1
    0 : 0 : 0    0 : 0 : 1    0 : 0 : 1
    1 : 0 : 1    1 : 1 : 1    1 : 1 : 0
    pointer = NULL

  5. #5
    Registered User Natase's Avatar
    Join Date
    Aug 2001
    Posts
    123
    Identical methinks

    Hehe... I take it that's a convention?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise operators
    By gnewfenix in forum C Programming
    Replies: 2
    Last Post: 05-16-2009, 08:43 PM
  2. Bitwise Operators
    By rrc55 in forum C Programming
    Replies: 6
    Last Post: 04-30-2009, 11:37 AM
  3. Palindromes and Bitwise operators
    By Dr Tornillo in forum C Programming
    Replies: 8
    Last Post: 08-02-2007, 02:31 PM
  4. bitwise and arithmetic Operators
    By Whiteghost in forum C Programming
    Replies: 4
    Last Post: 12-28-2006, 01:13 PM
  5. Bitwise Operators, Help!!
    By Mini__C in forum C Programming
    Replies: 6
    Last Post: 07-14-2004, 04:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21