bit manipulation - Bitwise arithmetic, How can x*x<0? -


i'm looking on reviewing exam involving bitwise operations , found useful page problem confused about. http://www.cs.cmu.edu/afs/cs/academic/class/15213-f05/lectures/class03.txt

the specific problem @ bottom of page under 'puzzles'. given signed integer x, , assuming 32 bit word size , 2's complement how can show

x*x <0?

they list 50000 possible solution, , not sure why or how works.

i tried starting small considering 3*3 = 9, 0011 * 0011 = 1001. moved 2 nibbles, 0010 0001 * 0010 0010 = 34*34 = 1156 = 100 [1000 0100] , thought found pattern of increasing intervals between 1 bits not work when got closer 32 bits.

can point me in right direction?

in 2's complement, most-significant bit denotes sign of number. if arithmetic operation positive numbers ends carrying specific bit, you'll end negative number.

for example:

         50 000 = 0b00000000000000001100001101010000 50 000 * 50 000 = 0b10010101000000101111100100000000 

the significant bit set 1, resulting number considered negative when interpreted signed value.

here's quick demo of above idea in action: https://ideone.com/tu049l


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -