1

Newbie on C language. I understand pointers and hex format but I'm not sure what '& 0x10' is doing. Could someone kindly explain it or advise me on topics to research and teach myself. Thank you.

if(ptr1->name & 0x10)
{
     prt2->indicator1  |= 0x80;      
}  
Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
Krn
  • 13
  • 3
  • 4
    Do you know about bitwise operators? – Eugene Sh. Sep 18 '18 at 22:18
  • Is `prt2` a typo? This code does a bitwise or with `0x80` and `prt2->indicator1` as operands and stores the result in `prt2->indicator1`. I'm not sure how to be more concrete than that without knowing what `prt2->indicator1` is. – ggorlen Sep 18 '18 at 22:20
  • if `ptr1->name` has its 5th bit set (or "bit 4" from zero-based), set the 9th bit of `ptr2->indicator`. Typically these will be "flags" that say something is or isn't true. – zzxyz Sep 18 '18 at 22:25

1 Answers1

0

I'm not sure what '& 0x10' is doing. Could someone kindly explain it or advise me on topics to research and teach myself. Thank you.

if(ptr1->name & 0x10)

Is taking whatever the value of ptr1->name is and bitwise-ANDing it with 0x10.

If you understand hex, then you know that 0x10 is 16 in decimal, and presumably you know that 16 is 2^4 which means in binary this value is 0b10000.

If the result of this operation is non-zero, then this tells us that the 4th bit of ptr1->name is set (bit numbering starts at 0, e.g 2^0 = 0x1), and will result in the execution of the line prt2->indicator1 |= 0x80;

Check out bitwise operators in C

bigwillydos
  • 1,321
  • 1
  • 10
  • 15