bitwise operator

This is a discussion on bitwise operator within the C Programming forums, part of the General Programming Boards category; hello guys, i am new to this, i hope u will all help me out to learn more about C. ...

  1. #1
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682

    Unhappy bitwise operator

    hello guys, i am new to this, i hope u will all help me out to learn more about C. i been looking into the bitwise operator in my test book. i got confused with one programe where i couldn't able to understand the logic and here is the programe

    Code:
    #include<stdio.h>
    #define SEED 3254
    
    int random()
    {
        static int reg=SEED;
        int bit13, bit14;
        int exor;
        
        bit14=(reg|0x4000)>>14;  // i am confused here
        bit13=(reg|0x2000)>>13;    exor=bit14^bit13;
        reg=reg<<1;
        
        reg=(reg(|exor)&0x7fff; // and here    
        return reg;
    }
    this is a fucntion to return the random number which it generates.
    my question is how does it access the 14th bit and the 13 bit. i couldn't able to get the above logic can any one explain me this please, waiting for your reply

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Add some debug lines, and write the hex values as binary if it helps. (And it is best to use unsigned types with bit shifting.)
    Code:
    #include<stdio.h>
    #define SEED 3254
    
    unsigned int random()
    {
        static unsigned int reg=SEED;
        unsigned int bit13, bit14;
        unsigned int exor;
        
        bit14=(reg|0x4000)>>14;  // i am confused here
        bit13=(reg|0x2000)>>13;    exor=bit14^bit13;
        printf("reg = %x, bit14 = %x, bit13 = %x\n", reg, bit14, bit13);
        reg=reg<<1;
        printf("reg = %x, bit14 = %x, bit13 = %x\n", reg, bit14, bit13);
        
        reg=(reg|exor)&0x7fff; // and here    
        printf("reg = %x, bit14 = %x, bit13 = %x\n", reg, bit14, bit13);
    
        return reg;
    }
    
    int main(void)
    {
       random();
       return 0;
    }
    
    /* my output
    reg = cb6, bit14 = 1, bit13 = 1
    reg = 196c, bit14 = 1, bit13 = 1
    reg = 196c, bit14 = 1, bit13 = 1
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    thax Dave_Sinkula i got an idea of what it is and how
    but still if you can see this
    Code:
    bit14=(reg|0x4000)>>14;  // i am confused here
        bit13=(reg|0x2000)>>13;
    
    reg=(reg|exor)&0x7fff;
    is this values specific or just a random values. while i debug i just used values as blind
    can any one explain me this please

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    bit14=(reg|0x4000)>>14;
    This sets a bit, it does not test a bit.

    If you just want to extract a single bit from a value, and return that bit as 0 or 1, then do
    bit14=(reg&0x4000)>>14;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    thax very much for all your help, and i have one more question to be clarified. the above code picks up the 14th and the 13th bit, but how to pick up the 10 or any in between bits from the byte. can any one tell me this please

    thax very much

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Hm... they use the numbers 14 and 13... Somehow this corresponds to the 14th and 13th... I wonder if using a different number could mysteriously correspond with another bit?


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

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    FAQ > Explanations of... > Bit shifting and bitwise operations
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > I wonder if using a different number could mysteriously correspond with another bit?
    LMAO - you have to wonder how they got to 14 without passing 10?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    I don't think he understands what the mask value means for the &.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. C bitwise operator exercise - more like newb killer
    By shdwsclan in forum C Programming
    Replies: 3
    Last Post: 02-22-2006, 06:02 AM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  5. Replies: 5
    Last Post: 10-30-2002, 09:23 PM

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