Bitwise Operations in C: setting bits from left -


given integer 10, how write 10 1 bits (starting left) in total of 16 bits so:

11111111.11000000 

or given integer 4, write:

11110000.00000000 

thanks, i'm still learning c , not familiar bitwise operations.

-(1 << wordsize - numbits) ought it.

it's instructive see happens in example. 1 << wordsize - numbits 1 << 12, 00010000.00000000. recalling -x = ~x+1, compute ~(1 << 12) = 11101111.111111. add 1 , 11110000.00000000.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -