-2

I have a char array. Some of the elements allocated, and some are not. I want to count all those not allocated without asking if (!arr[i])

char* arr[500];
.
.
.
for(i=0; i < 500; ++i)
{
   if( !arr[i] )
   { 
      counter++;
   }
}

but making some bitwise magic like this in C

for(i=0; i < 500; ++i)
{
    counter += !(arr[i]| 0x00); //this does not work 
}

Maybe the question is more general: is there a way to compare any pointer with NULL with bitwise operations.

Edit: no, not with ==; I understand how to do it with ==, but I assume when it is compiled, it does not differ from if(!arr[i]) in assembler.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Dabo
  • 2,371
  • 2
  • 18
  • 26
  • 4
    Why not just `counter += (arr[i] == NULL)`? – fasked Dec 30 '13 at 13:46
  • What "bitwise"? Why do you even... I mean, it doesn't make sense. –  Dec 30 '13 at 13:48
  • @fasked I assume == translated into something similar to if(!arr[i]) – Dabo Dec 30 '13 at 13:48
  • possible duplicate of [How to calculate the non-zero elements in the array?](http://stackoverflow.com/questions/20839793/how-to-calculate-the-non-zero-elements-in-the-array) –  Dec 30 '13 at 13:49
  • It seems that it's homework/finals time. –  Dec 30 '13 at 13:49
  • (And nobody can be bothered to do their own friggin' homework.) –  Dec 30 '13 at 13:49
  • Bitwise i'm asking, how to do it in bitwise,not with == – Dabo Dec 30 '13 at 13:52
  • 1
    @DavidBo It's not possible, because `NULL` is not has to be represented with all zero-bits. – fasked Dec 30 '13 at 14:16
  • Step back, think _gloves_... isn't `if (!arr[i])` easy to read? why would you want to make life more complicated than it already is? – Elias Van Ootegem Dec 30 '13 at 14:43
  • wanted to know if it is possible after reading this http://stackoverflow.com/a/11227902/2549281 – Dabo Dec 30 '13 at 14:50
  • @fasked: the null pointer *constant* is always 0-valued, hence the `NULL` macro is always going to be zero-valued (6.3.2.3/3 plus footnote 66); the corresponding null pointer *value* does not have to be zero-valued, but in the context of the source code, you're dealing with the null pointer *constant*. Having said that, `!arr[i]` or `arr[i] == NULL` are both safer than using a bitwise operation. – John Bode Dec 30 '13 at 14:58
  • @JohnBode Yes, that is what I meant. `0` integer *value* and `NULL` pointer *value* may be not equal bit by bit. – fasked Dec 30 '13 at 15:19
  • @DavidBo In your "this does not work loop", you might need to explain how it doesn't work. Since `x | 0 == x`, your `arr[i] | 0x00 == arr[i]`, so the code is no different than `counter += !(arr[i])`... – twalberg Dec 30 '13 at 15:30
  • @twalberg compiler does not allow to make bitwise OR between pointer and 0. it gives error : invalid operands to binary | (have ‘char *’ and ‘int’). So i wondered if there still some syntax that allows it. – Dabo Dec 30 '13 at 15:38
  • @DavidBo Ah, right... There is specific syntax to force it to work (hint: casts), but, as I mentioned, because `x | 0 == x` always, there's not really any point in it. The compiler would likely generate nearly identical code to your working example, even if you did force it - it would likely know that there's no point to OR with a constant 0, and so would optimize it away... – twalberg Dec 30 '13 at 15:46
  • @twalberg it actually did work, thanks for your hint. for(i=0; i < 500; i++) { counptr += !( (int)arr[i] | 0x0 ); printf("%d\n",counptr); } – Dabo Dec 31 '13 at 08:48
  • Dabo, please explain how reading the branch prediction question made you think that bitwise operations are superior? – Leeor Feb 28 '14 at 13:46
  • @Leeor not superior, but in the answer to the linked question presented bitwise technique reduced execution time, because `if` was eliminated from the loop. – Dabo Feb 28 '14 at 15:14

1 Answers1

3
for(i=0; i < 500; ++i)
{
    counter += arr[i] == NULL;
}

Or even...

for(i=0; i < 500; ++i)
{
    counter += !arr[i];
}
mcleod_ideafix
  • 11,128
  • 2
  • 24
  • 32
  • I suspect that casting to `bool` is not portable to systems with non-zero NULL pointers. – user694733 Dec 30 '13 at 13:57
  • 1
    @user694733 However, `!arr[i]` is always true if `arr[i]` is a `NULL` pointer, regardless to the actual numeric value of the null pointer. –  Dec 30 '13 at 14:07