GIDForums  

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-Oct-2009, 00:16
goliwala goliwala is offline
New Member
 
Join Date: Oct 2008
Posts: 6
goliwala has a little shameless behaviour in the past

Bit conversion


Hi 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:
import java.util.Scanner;

//I have to use short.  Its one of the requirements.
public class testConversion
{
    static Scanner scan = new Scanner (System.in);
    public static void main(String[] args)
    {
        System.out.print("Enter a integer: ");
        short x = (short) scan.nextInt();
        System.out.print(clcValue(x));
    }
    public static short clcValue(short memadd)
    {
        short memval = 0;
        short a,y;
        short num[] = new short[16];
        //short returnVal = 0;
        a = 0; 
        while(memadd!=0) 
        { 
            y = (short) (memadd % 2); 
            memadd = (short) (memadd / 2); 
            num[a]= y; 
            a++; 
        }
        a--;
        for(a = (short) (num.length - 1); a >= 0; a--)
        {
            if (num[8] == 0)
            {
                for(short i = 8; i <= 11; i++)
                {
                    //                    num [i] = (short) ~(num [i]); // no worki
                    if (num [i] == 0)
                    {
                        num [i] = 1;
                    }
                    else
                    {
                        num[i] = 0;
                    }
                }
            }
            else if (num[8] == 1)
            {
                for(short i = 12; i <= 15; i++)
                {
                    //    num [i] = (short) ~(num [i]);  // that did't work
                    if (num [i] == 0)
                    {
                        num [i] = 1;
                    }
                    else
                    {
                        num[i] = 0;
                    }
                }
            }
            System.out.print(num[a]);  // should invert selected bits
        }
        for (int i = 0; i >= num.length; i--)
        {
            memval = (short) (memval + num[i] * Math.pow(2, i));
        }
        System.out.println(memval); // just checking the output
        return memval;
    }
}
Last edited by LuciWiz : 14-Oct-2009 at 01:23. Reason: Please insert your Java code between [java] & [/java] tags
  #2  
Old 14-Oct-2009, 08:51
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,140
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: bit conversion


It's not 100% clear (to me, anyway) exactly what is the goal?

Say I input 15, should I expect to see this:
Code:
[0..3] [4..7] [8..11] [12..15] //'bit' ranges 1111 0000 0000 1111
...or is there something else that is supposed to happen? Please provide a sample input/output expectation?

Also, this loop:
JAVA Code:
    for (int i = 0; i >= num.length; i--)
...will NEVER execute, as 0 is not greater than 16.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 15-Oct-2009, 00:55
goliwala goliwala is offline
New Member
 
Join Date: Oct 2008
Posts: 6
goliwala has a little shameless behaviour in the past

Re: bit conversion


Thanks 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  
Old 15-Oct-2009, 01:09
goliwala goliwala is offline
New Member
 
Join Date: Oct 2008
Posts: 6
goliwala has a little shameless behaviour in the past

Re: bit conversion


OK, 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:
import java.util.Scanner;
public class testConversion 
{
	static Scanner scan = new Scanner (System.in);
	public static void main(String[] args)
	{
		System.out.print("Enter a integer: ");
		int x = scan.nextInt();
		System.out.print(clcValue(x));
	}
	public static int clcValue(int memadd)
	{
		int memval = 0;		
		if ((memadd & 0x00000100) == 0)
		{
			memval = (memadd ^ 0x00000f00);
		}
		else
			memval = (memadd ^ 0x0000f000);
		return memval;
	}
}
Last edited by admin : 17-Oct-2009 at 04:06. Reason: Please insert your example Java codes between [JAVA] and [/JAVA] tags
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 22:14.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.