JScript Bitwise Operations

Discussion in 'Programming' started by NileshRanjanRoy, Jul 16, 2009.

  1. #1
    I wrote some bitwise code that would convert all negatives into -1 and all positives into 1:

    var sign = (~((num >>> 30 & 2) - 1)) + 1; //negative -> -1, positives -> 1
    //0 becomes 1



    Firstly, is there a more efficient way to implement this?
    Secondly, I was wondering if the size of the variable is machine dependent. Right now, I'm shifting 30 places to the right assuming that the variable (integer) is 32 bits long. If its size is changes, then id have to determine the sizeOf variable.
     
    NileshRanjanRoy, Jul 16, 2009 IP