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.
Loading
VulpesPosted Apr 14, 2011, 5:47 AM
Miguel MartorellPosted Apr 14, 2011, 1:04 PM
Sam Hobbs : I'm sorry about that I knew it could be confusing or not have the wole information. That's why I explained it in more detail on the second post.
Still I appreaciate the help. Next time i'll try to give a more comprehensive explanation of my problem
Thank you all and have a great day.
Sam HobbsPosted Apr 14, 2011, 12:27 PM
Note that it is more important to describe the problem than it is to describe your guess at a solution.
VulpesPosted Apr 14, 2011, 11:56 AM
Not as hot on this stuff as I once was :(
Miguel MartorellPosted Apr 14, 2011, 11:04 AM
Now I can use this logic in other operations.
Take care and thanks once again for the help.
VulpesPosted Apr 14, 2011, 10:57 AM
Miguel MartorellPosted Apr 14, 2011, 10:38 AM
Thank you very much.
By any chance can you explain a few things?
I can't firugre out how you came up with the &7 to get int a.
I mean I just did is take the hex substring (5,3) and converted to int and it worked but that seems easier. Tried out logic but not working.jeje . I want to know for in case others ask on why I took that option.
And the same goes with a = (BE >> 3) & 31; when it's less that 128.
How did u come up with it.
The only way to inverse the bits id with a for function. What exactly does the Not (~) operation do? Thsi si where i was getting stuck. Should have tried the shifting bits manually like you did.
I really appreaciate the help. This worked very well.
Sam HobbsPosted Apr 14, 2011, 1:18 AM
Sam HobbsPosted Apr 14, 2011, 12:58 AM
Miguel MartorellPosted Apr 13, 2011, 11:51 PM
The data that is read for example is 090B 35BE FF00 (hex).
09 is a code which is 09 in dec
0 is a parameter which is 0 dec
B is another parameter which is 11 in dec
3 is another parameter which is 3 in dec
5 is another parameter which is 5 in dec
BE conatains two parameters. To get the you convert to binary and the last 3 bits is one data (goes from 0 - 7)
and the first 5 bits is a signed int that goes from -16 to +15.
Now, in the first post i covered how in paper i get to the answer. I just dont get how to get that part in c#. I got all the rest of the values by converting hex to int.
but that part is different. (twos complement i believe)...if you need more examples id be glad to put more
Thank you for your help.
Sam HobbsPosted Apr 13, 2011, 9:45 PM
Use the Windows Calculator accessory to try things out. Change the Calculator's view to Programmer. Put in the value 23. Click the radio button for Hex; you will see 17. Then click back to Dec. Click the "+-" button to make it negative. Then convert that to Hex; you should get FFFFFFFFFFFFFFE9. Do you understand why?
So I am not sure what the input data is. I am not sure whether there is a sign bit; since you say the output could be or should be negative, I am not sure about the sign bit.