How to set each bit, if the variable is short

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jtnshah
    New Member
    • Jan 2013
    • 1

    How to set each bit, if the variable is short

    i am using the below logic. but it gives me error when i am setting the bit_position 15 or 16. i suspect, the problem with the integer range.
    please suggest.
    Code:
    short bit_map, bit_position
    
    bit_map = bit_map | ( 1 << bit_position )
    Last edited by Meetee; Jan 3 '13, 10:55 AM. Reason: code tags <code/> added
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    A sort is 16 bits. Shifting 1 left by 16 bits won't work because the short is too small. Shifting 1 left 15 bits destroys the short's sign bit. Since most negative values are stored in 2's complement form, a 1 in the sign bit will cause the value t appear as a very large negative number. But you can avoid the sign bit problem by using an unsigned short.

    Comment

    • liamtosurvive
      New Member
      • Jan 2013
      • 4

      #3
      I think U can use ':'the do this!

      Comment

      Working...