c - At Least One bit in int num is 0 -


trying create method returns 1 if input int num has 0 in using c without using if-else statements , using bottom bitwise , boolean operators.

i tried far:

int hasazero(int num){  return !(num && 1); }  bitwise , (c = & b) – c has 1s in places both of    corresponding bits in , b 1. bitwise or (c = | b) – c has 1s wherever @ least 1 of corresponding bits in , b 1. bitwise xor (c = ^ b) – c has 1s wherever 1 , 1 of corresponding bits in , b 1. bitwise not (c = ~a) – c a, each bit inverted, else c 0. right shift (c = >> b) – c a, each bit moved lower b places. left shift (c = << b) – c a, each bit moved higher b places. boolean , (c = && b) – c 1 if both , b non-zero. boolean or (c = || b) – c 1 if either , b non-zero. boolean not (c = !a) – c 1 if 0. 

try this:

int hasazero(int num) {     return ~num != 0; } 

if num contains 0 bit, bitwise complement contain 1 bit , non-zero. if not contain 0 bit, complement zero.

if don't want use != can use !!~num instead rpattiso pointed out (i wasn't clear comparisons allowed), i.e.:

int hasazero(int num) {     return !!~num; } 

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -