binary « Integer « Java Data Type Q&A

Java Data Type Q&A
9.Date Time
10.Date Time Format
19.Number Format
Java Data Type Q&A » Integer » binary 

1. When casting a small integer type to a wider one, is it safe to rely on &ing with a mask to remove the sign?

I have code that stores values in the range 0..255 in a Java byte to save space in large data collections (10^9 records spread over a couple hundred arrays). Without additional measures ...

2. Converting an int to a binary string representation in Java?

What would be the best way (ideally, simplest) to convert an int to a binary string representation in Java? For example, say the int is 156. The binary string representation of this ...

3. How to get 0-padded binary representation of an integer in java?

for example, for 1, 2, 128, 256 the output can be (16 digits):

I tried
String.format("%16s", Integer.toBinaryString(1));
it puts spaces for left-padding:
`             ...

4. binary string to int

I have to convert binary string to integer. I can use & | << and >>. No exponentiation, no embedded functions.

5. Binary Integer Program Solver for Java

My problem is in trying to solve a Binary Integer Program through Java. I want to run a series of experiments and an integral component of these experiments is to ...

6. Obtaining reversed Integer in Java

There is a method in Java that reverses bits in an Integer reverseBytes(). I wanted to try another implementation and this is what I have:

public static int reverse(int num) {


7. represent an integer using binary in java language

Here is the problem: You're given 2 32-bit numbers, N & M, and two bit positions, i & j. write a method to set all bits between i and j in N ...

8. how to read signed int from bytes in java?

I have a spec which reads the next two bytes are signed int. To read that in java i have the following When i read a signed int in java using the following ...

9. int to binary ??

As Norm hints, the binary representation of an int only has meaning if you are displaying it or if you need to manipulate a String. While it is a great learning exercise to implement this yourself, the Integer class provides a toString() method that allows you to convert an int into a String representation in any base. Integer also provides convenience ...

10. String to Binary to Integer

11. How can i give binary value to int?

Originally posted by Ravindranath Chowdary: Hi, I can't able to get the int from the binary with the convertToBinary("10001000001001"); Can you please clear my doubt. Thanks, Ravindranath. First, If you read the previous post again, you'll notice that it is Pseudo Code, and that you should look at Integer.parseInt() for more information. Second, please don't do this... To create a new ...

12. Funny glitch while converting from int to 32 bit binary.....

Hi guys, I'm facing a strange problem. I know an int in Java is 4 bytes or 32 bits long and is represented in the 2's complement form. As per my understanding, a value of 128 should thus be represented as: 00000000 00000000 00000000 10000000 - ie 4 bytes long. I used the following program to extract this bit by bit: ...

13. convert integer to binary

14. integer to binary

You don't really need 5 variables. Code is good - but mostly to show people what attempt you have made and how you are thinking. That code does neither. Also, as prometheuzz has said: ask a question. Again code showing some attempt will help you do that. (If it comes up with a compiler message, or gives a runtime error, or ...

15. Converting an Integer to a Binary String

16. binary to int

Hi, Probably it's that i'm too tired.. and i need to get some sleep.. but i was trying to translate a string representing a binary number into an int.. I'm getting a NumberFormatException.. (...) 1 newBinaryRep = newBinaryRep.concat(tmpStringRep); 2 } // end of 'for' loop 3 outputPermuted = Integer.parseInt(newBinaryRep, 2); 4 return outputPermuted; Here's the String value at line 1: "11100101101110100010010101001101" ...

17. Binary string to integer

1.) replace that Pattern.compile.split() line with "a.toCharArray()" (of course replacing "String[] ss" with "char[] digits") 2.) let i start at "digits.length-1" instead of 4 2.) Don't write separate cases for each value of i: The value you add for each case is equal to "1 << i". Use that fact 3.) Replace your entire code with a call to Integer.parseInt(a, 2) ...

18. binary to integer?

i know there are methods for this but im trying to figure out a way to do it recursively. i know how to get an integer to binary recursively (split it up into its remainder and it divided by 2), but im having a lot of trouble going the other way. any ideas? thanks!

19. Problem converting int to binary.

Hi all... i got some problem here and hope some expert can help me... below is part of my coding... i had simplify it. int a= int b XOR char c String d= d + Integer.toBinaryString(a); the problem now is that the length of string d is not constant. sometimes the length is 8 sometimes its 6. how do i make ...

20. binary representation of an integer from String to byte in encoding

I am facing the problem of converting the binary string representation of integer to a byte... for example number 5-"101" can be stored in one byte rather than 3 bytes. so how can i do this.. My piece of code... String s = Integer.toBinaryString(int i); I have to convert that string s into one byte if it is <128 and 2 ...

21. Convert IEEE754 to binary integer

It's not a compilation failure, the problem is that the program seems to take the first 4 bytes of a double number and deprecate the remaining 4. Most of my data have four trailing zero bytes, i.e., they are really single-precision values, so when I make the conversion I only obtain zeros: 00 00 00 00 00 23 bd 40 -> ...

22. Conversion int <-> binary

Integer.toBinaryString() returns a string representation of the integer argument as an unsigned integer in base 2 from the API doc toBinaryString public static String toBinaryString(int i)Returns a string representation of the integer argument as an unsigned integer in base 2. The unsigned integer value is the argument plus 232 if the argument is negative; otherwise it is equal to the argument. ...

23. Small problem - int to binary...

Hey all, I need to create a simple java applet which converts an int into it's binary representation. Here is the output i need: Enter an int: (user enters int) That number in binary is: Thats all I need. I know it has something to do with a "wrapper class", but thats all I know. :P Could anyone point me in ...  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.