My program should convert decimal numbers to binary. For big numbers it is giving me a negative number not a binary number. Why is this?

For example, if I supply `2321`

I get `100100010001`

, which is fine. But if I supply `241242141`

I get `-2127232070093227171`

.

I can't use strings, arrays, functions. There is another option without define it as string? the output?

```
import java.util.Scanner;
public class d {
public static void main(String[] args) {
long num = 0;
long temp = 0L;
Scanner sc = new Scanner(System.in);
num = sc.nextLong();
long place = 1L;
long output = 0;
//System.out.print(""+ num%2+ (num%2)%2);
while(num != 0) {
temp = num % 2;
num = num / 2;
output += (place*temp);
place *=10;
}
System.out.print(""+output);
}
}
```

You problem is here

```
output += (place*temp);
place *=10;
```

this is producing a number which overflows.

A simple alternative is to create a String instead of generating a number you will convert to a String anyway.

```
StringBuilder output = new StringBuilder();
while(num != 0) {
output.append(num & 1);
num >>>= 1;
}
System.out.print(output.reverse());
```

or even

```
StringBuilder output = new StringBuilder();
for(long num = sc.netLong(); num != 0; num >>>= 1)
output.append(num & 1);
System.out.print(output.reverse());
```

**If you want to use no functions except input or output.**

```
long num = 241242141;
int shift = 63;
while (num >>> shift == 0 && shift > 0) shift--;
for (; shift >= 0; shift--)
System.out.print((num >>> shift) & 1);
// for comparison only
System.out.println("\n"+Long.toBinaryString(num));
```

prints

```
1110011000010001000000011101
1110011000010001000000011101
```

The problem is that, you are storing your `Binary Equivalent`

in a `long type`

, which cannot store such a long values.

You should rather use a `StringBuilder`

and append your `remainder - temp`

in it. Then print it in reverse: -

```
StringBuilder builder = new StringBuilder();
while(num != 0) {
temp = num % 2;
num = num / 2;
builder.append(temp);
output += (place*temp);
place *=10;
}
System.out.println(builder.reverse());
```

If you don't need to use any `methods`

, then just use `String Concatenation`

, and then a loop to print the string in reverse: -

```
String builder = "";
while(num != 0) {
temp = num % 2;
num = num / 2;
builder += temp;
output += (place*temp);
place *=10;
}
for (int i = builder.length() - 1; i >= 0; i--) {
System.out.print(builder.charAt(i));
}
```

But, beware, this will create a large number of `String objects`

on `Heap`

. Also, here you are using a `charAt`

method, that you have to use.

With recursion:

```
public class d {
static void toBinaryString( long number )
{
if( number > 1 ) toBinaryString( number / 2L );
System.out.print( number % 2L );
}
public static void main(String[] args) {
long num = 241242141L;
System.out.println( Long.toBinaryString( num ));
toBinaryString( num );
}
}
```

The ouput:

```
1110011000010001000000011101
1110011000010001000000011101
```

Similar Questions

I have a date encoded (big endian) in 4 bytes that I'm trying to read from a binary file. I do: char date[4]; long seconds; s = read(fd, date, sizeof(char) * 4); seconds = (date[3]<<0) | (date[2

I need to convert a 20digits decimal to binary using C programming. What buffer will I create, because it will be very large, even the calculator can't compute converting 20 digits to Binary. here is

I need help for a starting point, really. We must use these 3 loops to generate a table of decimal numbers, as well as the binary, octal, and hexadecimal equivalents of the decimal numbers, in the ran

I'm pretty new to scheme. I know how to convert from decimal to binary but I can't come with the logic of converting the other way around. I want to get a decimal number from a list of binary numbers.

I wrote this code for converting binary to text . public static void main(String args[]) throws IOException{ BufferedReader b = new BufferedReader(new InputStreamReader(System.in)); System.out.printl

Hey so I'm taking my first cs course ever at my university its taught in java. I'm having trouble converting decimal to binary. I seem to be able to get the correct output but its in reverse order, an

Is there a custom or standard library that I can use with objective c to convert a decimal floating point value into Hexadecimal, Binary and Octal NSStrings? I've been looking around a lot but can onl

In the coding I have done, I have a problem at the very end. When the program actually converts the decimal into binary form, the JOptionPane window separates each number in the binary answer. I don't

iv made a decimal to binary converter but I need a binary to decimal converter without using the bin function and that looks simple enough to fit with this code. can anyone help? choice=input('Please

I implemented a binary to decimal function in Haskell and am currently working on a function that would convert a decimal into a binary value. (I'm aware that these functionalities are available somew

I using string to contain my 64 bits binary. string aBinary; aBinary = 100011111011101100000101101110000100111000011100100100110101100; Initially i tried this.. stringstream ss; ss << bitset&

I am trying to read a txt file which has hexadecimal data. I Want to convert them in decimal except one column which I want to convert into binary bits and write them in 8 separate columns. Sample dat

please anyone help me to convert an image url to binary format and converted binary to image url..Iam using java ...please reply

I know how to convert binary to decimal. I know at least 2 methods: table and power ;-) I want to convert binary to decimal and print this decimal. Moreover, I'm not interested in this `decimal'; I wa

I have converted a decimal number to binary using int k=8; String toBinaryString = Integer.toBinaryString(k); After some manipulation I want the int value back. Can anyone help me with the function o

I have been studying Netty and Mina but am confused as to the best way to rewrite binary streams. For example, I would like to create a proxy that will allow for replacement of XML and forward along.

Hi I am trying to make a Decimal to binary number converter in Objective-C but have been unsucessful... I have the following method so far which is an attempted translation from Java for a similar met

Let's say I want to convert -128 into binary. From what I understand, I get the binary representation of 128, invert the bits and then add 1. So 128 = 10000000 So the inverse is 01111111 So and

Need a bit of help here: I was assigned a project to convert a Hex values in a file to Decimal using Java without using methods available in Java that can do this directly(we have to do it the long wa

What wrong with my code anyone help me plz .It's a decimal to binary conversion . According to my code , the out will be 2 for 10 , 3 for 11 but it output always add the last value at the end like for

I have defined data type for Binary numbers as follows data Bin = Nil | O Bin | I Bin deriving (Show, Eq) i want to define a function reverse :: Bin -> Bin so that when i give input like reverse (

I have two very large binary strings as below. How do I perform a bitwise xor on these big strings and display result in binary? string 1. 1000110100110000000010111011001011101111111011101011111010001

I'm a little confused on how to normalize numbers in C. I know that if you have something like the floating-point binary value 1101.101, it is normalized as 1.101101 x 2^3 by moving the decimal point

How can I convert four bytes of binary data (big endian) to an IEEE floating point value using C#?

I don't know how to convert a non terminating binary number(fraction) to decimal . Can anybody guide me how to do with an example?

I am trying to write a program that converts binary(with or without fraction) inputs into hex which is nearly done but unfortunately in the hex output the point (.)is missing. Suppose my expected o

I want to convert binary string in to digit E.g var binary = 1101000 // code for 104 var digit = binary.toString(10); // Convert String or Digit (But it does not work !) console.log(digit); How is

I'm trying to decode and read binary data sent from a device, I need to extract a decimal number which is inside the binary structure. this is the structure detail: (2) Synchronization message format

I like to scan larger(>500M) binary files for structs/patterns. I am new to the language an hope that someone can give me start. Actually the files are a database containing Segments. A Segment sta

If I'm being told that a specific function will return a vector of binary flags (32-bit int value), what does that mean? Can you give an example that demonstrates that? Thanks a lot.

I have a matrix with decimal elements. I want to convert every element in this matrix to binary with a resolution of 8. What function should I use?

I'm new to Java and trying to take a BigDecimal (for example 99999999.99) and convert it to a string but without the decimal place and trailing numbers. Also, I don't want commas in the number and rou

Is there any dedicated function for converting the binary values to decimal values. such as (1111 to 15 ) , ( 0011 to 3 ) . Thanks in Advance

I have a function that converts decimal to binary and returns a char * of a binary: void decimal2binary(int decimalNum, int size, char * charPtr){ int decimalTemp = decimalNum; int modNum; for(int siz

I am trying to modify a Recursive Binary Search function so that it will find the leftmost index of the element given the array contains multiples of that element. import java.util.*; import java.util

There have been several questions posted to SO about floating-point representation. For example, the decimal number 0.1 doesn't have an exact binary representation, so it's dangerous to use the == ope

Using Mathematica Binary String: FBCD #1 I: ImportString[FBCD, {Binary, Bit}] O: {0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, \ 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0} #2 I: Import

I am trying to create binary trees in JUNG (a graph package for Java). However, I cannot get manage to accomplish this. Here is my source: Tree tree1 = new OrderedKAryTree<String, Integer>(2); t

I know this seams to be easy but, I have a question to this particular situation. I already know how to convert decimal to binary using PHP, but I want to display the full bit sequence, but surprising

I have got a question, and it says calculate the tight time complexity for the process of inserting n numbers into a binary search tree. It does not denote whether this is a balanced tree or not. So

I am totally new to Python. I have to parse a .txt file that contains network byte order binary encoded numbers (see here for the details on the data). I know that I have to use the package struct.unp

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 solve an integer program where the va

I have a requirement where I am generating a binary number from a string say 111111 and converting it to a decimal number to store in database. Each digit (ie. 1/0) from this binary number signifies

This question already has an answer here: 2's complement hex number to decimal in java 3 answers I know that converting a decimal to binary with Integer.toBinaryString(355) = 0000000101100011 a

I'm trying to convert the number 101 to the binary \x00\x65 and I don't know how. I've tried this: $decimal = 101; return bin2hex(pack('S', $decimal)); But the result is 6500. The number is going to

I have a java REST web service that is currently downloading text files to an iPad application. We are wanting to extend the service to now download 10 - 20 Mb of binary data at one time from multiple

I would like to convert floating numbers (eg. 152.35964) between binary, octal, decimal and hexadecimal numbers. I googled but I didn't found anything relative to what I want. All that I have found wa

I am trying to inter-convert some decimal and binary numbers. I am working with data produced in the following format: Example decimal: 163, Corresponding binary: 10100011 Binary table key: ...and t

I'm trying to convert binary data (string) to hexa decimal data (string) string BinaryData = 1011000000001001001000110100010101100111100000000001000001111011100010101011; string HexaDecimalData = Co

I am trying to simply convert to Binary with recursion. I am having problems with the return statement. This compiles but give an overflow error when run. I don't know what to return (or if my stateme