1

I don't understand what the third line is trying to accomplish. I just recently learned bit-wise operators. It would be great if someone could walk me though the last two lines. I understand shift operator but i.t.o the shift operator I'm not entirely sure of what it means.

    void create(uint8_t bInt[], int64_t num){
      for (int pos = 0; pos < 32; pos++){ 
        bInt[pos] = (num & mask) ? 1 : 0;
        mask = mask << 1;
         }
       }

For this assignment, we're using 32-element array of uint8_t values to represent 32 bit integers. For example, the integer 84193 in binary is 0....0001 0100 1000 1110 0001. In bInt[], it would be stored as 1000 0111 0001 0010 1000 0000....0. Thank you for your time

ponderingdev
  • 331
  • 1
  • 6
  • 15

2 Answers2

7

?: is a ternary operator. (num & mask) ? 1 : 0;

Think of it like this:

if( (num & mask) ) {
    bInt[pos] = 1
} else {
    bInt[pos] = 0
}
Ryan
  • 14,392
  • 8
  • 62
  • 102
1

It is ternary operator and also used in some other language like - java, c++. It is a replacement of short form if-then-else. It works like below -

expression ? if_true_then_process_it : or_process_it  
Razib
  • 10,965
  • 11
  • 53
  • 80
  • 2
    Hmm.. Avoid repeating answers already given in repose to a question. While correct, self, provided the exact answer 5 minutes before your response. Sometimes, it is unavoidable, (e.g. two people answer at the same time), but it is something that should be avoided if possible. – David C. Rankin Mar 23 '15 at 22:34