11
Answers

Help with bit wise opetation to get two values from a hex#

Ask a question
Hi,
I have a program where I have to get two values from a HEX#

For ex. BE which in binary is equal to 10111110

Now the structure to get the two values is this:
The last 3 bits is one value.

So 110 = 6 <---I got this.

Here comes my problem.

The first 5 bits is a 5 bit signed integer and I can't seem to get it to work in c#.

The 5 bits in this example are 10111
On paper what I do it do the 1's complement and add 1. This has given me the value I need (the correct value).

Regular value = 10111
1's Comp = 01000
Add 1 = 01001 <---- = -9 since the first bit is a 1 in the regular value which makes it negative

My problem is transalting this in C#. I Changed the value to an int which would be 23.

Lets say a = 23.
How do I get from here to -9? The ~a gives me -24 and obviosuly adding 1 will give -23. Can anyone help? If you have any questions, dont hesitate to ask. Hopefully this was clear.

Answers (11)