The Artima Developer Community
Sponsored Link

Java Answers Forum
Reading in Binary Numbers

6 replies on 1 page. Most recent reply: Nov 15, 2002 7:51 AM by Dante T. Salvador

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Ger Deasy

Posts: 4
Nickname: gerd
Registered: Nov, 2002

Reading in Binary Numbers Posted: Nov 14, 2002 3:51 AM
Reply to this message Reply
Advertisement
I'm writng a program to use for the manipulation of binary numbers.

I have method to read in a decimal and return the binary equivalent, but I need a method to reverse a binary number which is specified in the input.

For example,
the output prints
"Please enter a binary number to reversed":
1101001
"Output should return 1001011"
However I only get a single 0 or 1
Question how can I read the binary number a get it to remain binary???

All suggestions welcome


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Reading in Binary Numbers Posted: Nov 14, 2002 9:39 AM
Reply to this message Reply
I think if you look just a few topics down the list, you'll find a solution to reversing a String (both recursive and non-recursive methods, no less), which is all this amounts to.

Daniel Ray

Posts: 53
Nickname: budoray
Registered: Oct, 2002

Re: Reading in Binary Numbers Posted: Nov 14, 2002 12:16 PM
Reply to this message Reply
Using a StringBuffer will give you acces to a reverse() method.

Ray

Ger Deasy

Posts: 4
Nickname: gerd
Registered: Nov, 2002

Re: Reading in Binary Numbers Posted: Nov 15, 2002 3:35 AM
Reply to this message Reply
I've tried this already but the method is part of a lab assignment, and must take an int as its input parameter and return an int. I've tried parsing the int to a String and reversing it inside the method but if I take in the number 00001010 reversing it gives 1??????

public static int reverseBits( int x )
{
// code here
}

Dante T. Salvador

Posts: 19
Nickname: anteng
Registered: Nov, 2002

Re: Reading in Binary Numbers Posted: Nov 15, 2002 3:41 AM
Reply to this message Reply
can you show some more code.

Ger Deasy

Posts: 4
Nickname: gerd
Registered: Nov, 2002

Re: Reading in Binary Numbers Posted: Nov 15, 2002 4:18 AM
Reply to this message Reply
Heres the Program in Full, the method is reverseBits( int x )

public class BitDisplay
{
static void displayBits(int x){

int c, displayMask;
// Create display mask
displayMask = (0x1 <<15);

System.out.print(x + " = ");
// Extract each of the 16 bitsfrom x one at a time.
for (c = 1; c <= 16; c++){

// Determine whetherthe c'th bit from the left is a
// one or a zero
if ((x & displayMask)==displayMask)
{
System.out.print("1");
}

else
{
System.out.print("0");
}
// Shift x left by one bit.
x <<= 1;

}
}

static int FirstBit( int x )
{
int c, displayMask, bitPosition = 0;
boolean oneFound = false;

displayMask = (0x1 << 15);
c=1;
while(!oneFound)
{
if ((x & displayMask)==displayMask)
{
bitPosition = c;
oneFound = true;
}

else
{
oneFound = false;
}
// Shift x left by one bit.
x <<= 1;
c++;
}
return bitPosition;
}
static int GetBits()
{
KeyboardInput in = new KeyboardInput();
int decimalEquivalent = 0;
String binaryString;
System.out.print("Please enter a binary number");
binaryString = in.readString();
int j = 0;
for( int i = binaryString.length()-1; i > -1 ; i-- )
{

char character = binaryString.charAt( i );

if( character == '1' )
{
decimalEquivalent += (int)Math.pow(2, j);
}

else
{
decimalEquivalent += 0;
}
j++;
}
return decimalEquivalent;
}

static int reverseBits( int x )
{
int displayMask, binaryReversedInt;
String binaryAsString, binaryAsStringReversed;

displayMask = (0x1 <<15 );

binaryAsString = "";
binaryAsStringReversed = "";

for ( int i = 0; i <= 15; i++ )
{
if((x & displayMask) == displayMask )
{
binaryAsString += "1";
}

else
{
binaryAsString += "0";
}
}

int j = 0;
for ( int i = 15; i >= 0; i-- )
{
char character = binaryAsString.charAt( j );
binaryAsStringReversed += character;
}

binaryReversedInt = Integer.parseInt( binaryAsStringReversed );
return binaryReversedInt;
}

public static void main (String[] args)
{
int x,c;
KeyboardInput in = new KeyboardInput ();

System.out.print("Please input a decimal number:");
x = in.readInteger();
displayBits(x);
System.out.print("\nPosition of Most Significant Bit " +FirstBit(x) );
System.out.println();

int binaryEquiv = GetBits();
System.out.println("Binary Equivalent is " +binaryEquiv );
System.out.println();

System.out.print( "Please enter a binary number to be reversed" );
String inputAsString = in.readString();
int input = Integer.parseInt( inputAsString, 2 );
int resultOfReverse = reverseBits( input );
System.out.println( "The result of reversing " +input+ " is " +resultOfReverse );

}
}

Dante T. Salvador

Posts: 19
Nickname: anteng
Registered: Nov, 2002

Re: Reading in Binary Numbers Posted: Nov 15, 2002 7:51 AM
Reply to this message Reply
try this class


class SimpleBinary { //begin class
public String integerToBinary(int integerValue){ // begin
return Integer.toBinaryString(integerValue);//**
} // end
public String reverseBinary(String binaryNumber) { // begin
String reversedBinary = new String("");
int binaryLength = binaryNumber.length();

for (int i = binaryLength-1; i >=0; i--)
reversedBinary += binaryNumber.charAt(i) ;
return reversedBinary;
} // end
} // end class
public class BitDisplay { // begin class
public static void main(String[] args) { // begin main
SimpleBinary bin = new SimpleBinary();
int num = 10;
System.out.println("Binary representation of " + num + " : " +
bin.integerToBinary(num));
System.out.println("Reverse of binary string of " +
bin.integerToBinary(num) + " : " +
bin.reverseBinary(bin.integerToBinary(num)));
} // begin main
} // end class


**
Integer.toBinaryString


is built in API, try to read it.

Flat View: This topic has 6 replies on 1 page
Topic: Another complicated qiestion about JVM Previous Topic   Next Topic Topic: Trouble setting up MySQL JDBC

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use