Bitwise Operations in C: setting bits from left

bitwise-operators, c

Solution

`-(1 << wordsize - numbits)` ought to do it.

It's instructive to see what happens in your example. `1 << wordsize - numbits` is `1 << 12`, which is `00010000.00000000`. Recalling that `-x = ~x+1`, we compute `~(1 << 12) = 11101111.111111`. Add 1 and you get `11110000.00000000`.

Problem

Given an integer like 10, how could I write 10 1 bits (starting from the left) in a total of 16 bits like so: ``` 11111111.11000000 ``` Or given an integer like 4, it would write: ``` 11110000.00000000 ``` Thanks, I'm still learning C and am not familiar with bitwise operations.

Original source