bitwise operator

• 09-27-2005
ssharish2005
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
• 09-27-2005
Dave_Sinkula
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 */```
• 09-28-2005
ssharish2005
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
• 09-28-2005
Salem
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;
• 09-29-2005
ssharish2005
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
• 09-29-2005
quzah
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.
• 09-29-2005
Dave_Sinkula
FAQ > Explanations of... > Bit shifting and bitwise operations
• 09-30-2005
Salem
> 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?
• 09-30-2005
cwr
I don't think he understands what the mask value means for the &.