![]() |
|
#1
|
|||
|
|||
Bit conversionHi All,
Need help with following program. Trying to input decimal, convert to binary, flip certain bits (IF the value for the bits 11-8 is even, THEN reverse the value for the bits 11-8, ELSE reverse the value for the bits 15-12.) Then convert back to decimal. Please help, Here is the code I have come up with so far. Thanks. JAVA Code:
Last edited by LuciWiz : 14-Oct-2009 at 01:23.
Reason: Please insert your Java code between [java] & [/java] tags
|
|||
|
#2
|
||||
|
||||
Re: bit conversionIt's not 100% clear (to me, anyway) exactly what is the goal?
Say I input 15, should I expect to see this: Code:
Also, this loop: JAVA Code:
__________________
Use the force...read the source!! WYCIWYG -- what you code is what you get! |
|
#3
|
|||
|
|||
Re: bit conversionThanks for the reply.
I had realized the problem with for loop and corrected it. Now, I understand that this program can be much simpler then what I have so I am giving up on the original code. Here is what I am trying to do now: I have an input “memadd” which is 2 bytes (short). I check the eighth bit, if it is 0 then I flip (XOR) bits 8 – 11 but if it is 1 then I flip (XOR bits 12 – 15. I know this can be done like this; memval = memadd & 0x00000f00 == 0 // AND 8th bit in memadd with 1, I don't know if the code is correct. That is where I need help. memval = memadd ^ 0x0000f00. //If result is 0 then XOR memadd bits 8 – 1 with 0xf (1111). Else memval = memadd ^ 0x0000f000 // else XOR bits 12 – 15 with all ones and print out memval (decimal value). Just don’t know how to code it correctly. Example: If memadd value is 13088, which has a binary representation of: 0011 0011 0010 0000 so bits 11-8 are 0011, which is ODD, therefore bits 15-12 should be flipped, ie: from 0011 to 1100. the returned value 'memval' is therefore: the original memadd value with the bits 15-12 flipped or 1100 0011 001000 00 which is 49952 in decimal. |
|
#4
|
|||
|
|||
Re: bit conversionOK, I got it. just took me 5 minutes
what a difference between two codes. I wish I had known about bitwise operators earlier. Sorry I don't know how to make the code appear proper. JAVA Code:
Last edited by admin : 17-Oct-2009 at 04:06.
Reason: Please insert your example Java codes between [JAVA] and [/JAVA] tags
|
Recent GIDBlog
Once again, no time for hobbies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Problem executing nam-1.13 | RodolfoAlvizu | Computer Software Forum - Linux | 20 | 28-Feb-2009 16:23 |
| Linked Lists advice request | promsan | C Programming Language | 74 | 23-May-2007 09:29 |
| conversion member function question | amad1337 | C++ Forum | 1 | 13-Jun-2006 21:21 |
| conversion double to float | donaldk | C Programming Language | 5 | 13-Feb-2006 00:16 |
| C Currency Conversion program help needed | mutt | C Programming Language | 1 | 13-Jun-2004 16:14 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The