Binary numbers to decimal numbers

Hello, Im writing a program that changes binary numbers to decimal numbers. I seem to be having a probelm calculating the last number in the binary code and adding it to the total. Also, when I use large binary numbers the output isnt anywhere near correct. help? please? thank you

public class binarynumbers
public static void main (String [] args)
int decimal=0;
Scanner scan= new Scanner(System.in);
System.out.print("enter binary number> ");
String bin=scan.next();
int zero=0;
int one=1;
int length=bin.length();
int newlen=length-1;
while (newlen>=0)
     String number = bin.substring( zero , one );
     int number1=Integer.parseInt(number);
     int squareRoot=Math.pow(2,newlen);
     decimal+=(squareRoot*number1);
     newlen--;
     zero++;
     one++;
System.out.println("The decimal number translation is " + decimal);
i did that but the math part isnt working and im getting a message like this
binarynumbers.java:26: possible loss of precision
found : double
required: int
     int squareRoot=Math.pow(2,newlen);
^
1 error

Similar Messages

  • Adding binary numbers in java please help!!

    Hello all, im a total newbie to java and i need your help urgently, i have two variables
    that are integers, that store binary numbers i.e.
    int tmpIntOne, tmpIntTwo;
    tmpIntOne = 1010;
    tmpIntTwo = 1110;
    i want to add these numbers together and return a binary result so far when you add them together you get:
    2120
    however i would like a binary result so that
    result = tmpIntOne + tmpIntTwo;
    = 11000
    Thank you.

    Hello all, im a total newbie to java and i need your
    help urgently, i have two variables
    that are integers, that store binary numbers i.e.
    int tmpIntOne, tmpIntTwo;
    tmpIntOne = 1010;
    tmpIntTwo = 1110;These are not binary numbers. These are decimal numbers whose digits consist of ones and zeros. If you want to interpret the characters "010" as a binary number, then do
    int tmp1 = Integer.parseInt("1010", 2);
    etc.
    i want to add these numbers together
    int result = tmp1 + tmp;
    and return a
    binary result Adding two ints will give an int. Int's are always binary. If you want to display it as a binary String, as opposed to the usual decimal String, then, as suggested, use toBinaryString.

  • Operations on very large binary numbers

    Hi guys,
    I'm trying to write a java class for manipulations of very large binary numbers.
    I'm representing the BN internally as boolean[] (array of booleans false for 0 & true for 1).
    I want to write an algorithm for the following operations :
    shifLeft (boolean[] b,int n) : shifting binary number to left by n positions.preferably a circular shift .
    and also the corresponding shiftRight() method.
    can some one guide me on how to implement this ?
    thanks.

    Consider an array of ascii chars....
    array =>   | |a|b|c|d|
    offset = 1
    len = 4In the above there are actually five spots in the
    array but the offset points to the second position in
    the array and the length is 4.
    If I was to extract the value it would be "abcd"
    because of the offset and the length.
    Now a 'shift right' means that if it is "abcd" then
    it should now be "abc" (because the d fell off the
    end.)
    I can do that like this.
    array =>   | |a|b|c|d|
    offset = 1
    len = 3Notice in the above that nothing changed except the
    length. But because the length changed if I
    extracted the value I would get "abc" because the
    offset is 1 and the length is three.
    Notice also that there was no array copying.Are you sure shifting works like that ?
    what I know about left shifting is that the 'd' should be moved left -toghether with a, b, c- and we append a 0 in place of 'd' .
    from what you said the 'd' would be gone , or am I again wrong ?
    it seems i still didn't get any satisfactory answer to this problem...

  • Compare 2 Binary Numbers

    Hi all ,
    suppose I have two arbitrary lenght binary numbers (could be very large binaries).
    exple : 1000111 & 11111110001
    is it possible to compare them using only there 0s & 1s representations.
    i.e I don't want to convert them to base 10 & compare them (probably using BigInteger).
    what I'm looking for is an algorithm that compares 1000111 & 11111110001 only using there 0s & 1s sequences.
    If this is possible how can I implement it ?
    many thanks.

    boolean equal = true;
    for(inti = ar1.length, j=ar2.lenght; i >= 0 && j
    =
    0; i--, j--) {
    if(ar1[ i ]!=ar2[j]) {
    equal = false;
    break;
    } this code will tell you if ar1 & ar2 are equal but
    won't tell you if ar1 <ar2 or ar1>ar2
    how can we tell ar1 > ar2 for exple ?oh my bad.
    well then indeed one would hav to start at the highest bit and embed the "shorter" value with 0's.
    if the representation is in a primitive, eg. long, you cant simply write a < b since java doesnt know unsigned numbers. so either you are carefull enough and only store 31 bits in an int(like leave the highest bit alone) or you check the highest bit first, and then check the remaining.

  • Recursion with binary numbers

    I need to print out a list of all possiblle binary numbers with as many digits as an int k. For example, if k = 3, it would need to print out 000,001,010,011,100,101,110,111. for k = 2 it is only 00, 01, 10, 11. It is pretty clear that for any k, a recursive technique can be used to solve this problem, but I'm having trouble finding the solution. Please help. Thanks

    You don't need recursion:for (int i = 0, max = 1 << (k - 1); i < max; ++i) {
      print binary i to k digits
    }But a recursive approach would be void increment (int[] data, int digit) {
    if (digit > 0) {
    --digit;
    for (int i = 0; i < 2; ++i) {
      data[digit] = i;
      increment(data, digit);
    } else {
    print contents of data array
    start (int k) { increment(new int[k], k); }Neither code tested.
    Pete

  • Printing Binary Numbers in SP

    I am trying to print binary numbers in my output.
    Below is the expected output
    1 0x5F6AB40860296D4DB07C346FA12E25FD
    2 0x5F6AB40860296D4DB07C346FA12E25FD
    Below is the output i am getting
    -1590811137
    CREATE
    TABLE Testbinary(
    [ID] [int]
    NOT
    NULL,
    [yVolumeUnitID] [binary]
    (16)
    NOT
    NULL
    insert
    into Testbinary
    values(1,0x5F6AB40860296D4DB07C346FA12E25FD)
    insert
    into Testbinary
    values(2,0x5F6AB40860296D4DB07C346FA12E25FD)
    select
    from Testbinary
    delete Testbinary
    select yVolumeUnitID
    from dbo.MdPriceDesc
    alter
    proc vol
    as
    declare
    @ID
    int,
    @VolumeID
    binary(16),
    @out
    varchar(100)
    begin
    select @ID=ID,
    @VolumeID=yVolumeUnitID
    from Testbinary
    set
    @out = @ID+''+@VolumeID
    print
    (@out)
    end
    exec vol

    You have some implicit conversion going on. Your INT column is ADDED to your VARBINARY column by 
    @out = @ID+''+@VolumeID
    DECLARE @table TABLE (id INT, number VARBINARY(MAX))
    INSERT INTO @table (id, number) VALUES
    (1, 0x5F6AB40860296D4DB07C346FA12E25FD),
    (2, 0x5F6AB40860296D4DB07C346FA12E25FD)
    declare @vb VARBINARY(MAX), @int INT
    SELECT @vb = number, @int = id
    FROM @table
    PRINT @vb
    PRINT @int+@vb
    PRINT CAST(@int AS NVARCHAR)+''+master.sys.fn_varbintohexstr(@vb)
    Don't forget to mark helpful posts, and answers. It helps others to find relevant posts to the same question.

  • Strings of binary numbers to integers

    Im quite new to java and have been working on a data compression project as a student. I have two questions:
    Is there an easy way to request the number of bits a particular number will take up without any leading zero's. e.g. 7 = 3 bits
    Is there an easy way to convert a number stored in it's binary form as a string to its decimal equivalent.
    I currently have my own procedures to perform these functions but they require lots of string ops and I am looking to improve performance.
    Any help is appreciated.

    Im quite new to java and have been working on a data
    compression project as a student. I have two
    questions:http://java.sun.com/docs/books/tutorial/java/nutsandbolts/bitwise.html
    Is there an easy way to request the number of bits a
    particular number will take up without any leading
    zero's. e.g. 7 = 3 bits
    int numBits(int myInt)
       int bits = 32;
       for(int xor = (1 << 31) ; (xor & myInt) == 0 ; xor >>>= 1, bits--);
       return bits;
    Is there an easy way to convert a number stored in
    it's binary form as a string to its decimal
    equivalent.If you mean convert a binary String to decimal,
    String decimalString = Integer.toString(Integer.parseInt(binaryString));

  • How can I add three binary numbers and then convert to string (Hex format)?

    Hi All,
    I have my problem "half" resolved, but need some assistance on the final piece.  Overall, I need to take three string values (hex format), convert them to binary, add them and then convert back to a string (hex format).  If that doesn't make sense maybe this will:
    Final Result (My target) = 0xCC
     = 0x03 + 0x01 + 0x04
              = 0x03; 0011b
              = 0x01; 0001b
              = 0x04; 0100b
    For my app, we are dropping "masking" some of the bits so that we get 1 byte:
              = 0x03; 11b
              = 0x01; 001b
              = 0x04; 100b
    So...
    11001100b = 0xCC
    In my VI, I am taking each initial string [0x03,0x01 and 0x04] type casting them to U8.  Next I need to "mask" the binary values as shown above and then add them.  I am not sure how to do this in LV??
    Thanks for the help,
    Bryan

    Hi Bryan,
    there's a conversion palette in the string palette, ever looked at those functions?
    The rest is pure simple math:
    It's the straight-forward way, there are other possibilities too!
    Best regards,
    GerdW
    CLAD, using 2009SP1 + LV2011SP1 + LV2014SP1 on WinXP+Win7+cRIO
    Kudos are welcome

  • Need help with binary numbers

    hey i'm doing some things, but i need a entry of a binary coded number, i know that an hex number is written like 0xFF but how do i put a binary number in an instruction such as:
    byte b
    b="binary number"

    I see what you want to do, but you can only do this with octal and hex. With binary, you would have to do it something like this:
    byte b = Byte.parseByte("01101001", 2); // base-2

  • Printing hexadecimal/binary numbers

    I'm trying to print the hexadecimal and binary representation of the following:
    public static void main(String[] args) {
    int t = 0x1;
    t = t<<30;
    System.out.println(0xt);
    I tried to print the hexadecimal representation of t using the above, but it didn't work (I know t isn't a hex number, but I want the contents of variable t printed in hex). How can I accomplish this?
    Thanks ahead.

    Use the Integer class. It has methods for binary and hex:
    Integer.toBinaryString(int i);
    Integer.toHexString(int i);

  • Playing with binary numbers

    Hi
    I am messing around with classes. I have a card class, which has a rank (2 - 14) and suit (0-3). What I am trying to do, is have a variable in the class cardvalue which holds a binary value for each card, for example, the 2 (2) of hearts (0) would be 0010 00 (I will of course drop the space), I would like it formatted with the first four digits representing the rank, a the second two the suit, for simple comparison with other cards later on in the game.
    How do I take the card values and concatenate them together?
    Many thanks, Ron

    Thanks, I have this:
    [code
    public class Card
         public enum Rank
         Deuce(2),Three(3),Four(4),Five(5),Six(6),Seven(7), Eight(8), Nine(9), Ten(10), Jack(11), Queen(12), King(13), Ace(14);     
              private final Integer facevalue;
         Rank(Integer facevalue)
         this.facevalue = facevalue;
         public Integer facevalue(){ return facevalue; };
         public enum Suit
              Hearts (0), Clubs(1), Diamonds(2), Spades(3);
              private final Integer suitvalue;
              Suit(Integer suitvalue)
                   this.suitvalue = suitvalue;
              public Integer suitvalue(){ return suitvalue; };
         private final Rank rank;
         private final Suit suit;
         private Integer binaryvalue;//value of the card as a 6 bit number, first two suit, second four rank value
         private Card(Rank rank, Suit suit)
    this.rank = rank;
    this.suit = suit;
    //binaryvalue = rank.facevalue & suit.suitvalue;
    //binaryvalue = Integer(Integer.toBinaryString( rank.facevalue));
         public Rank rank() { return rank; }
    public Suit suit() { return suit; }
    public Integer binaryvalue(){ return binaryvalue; }
    public String toString() { return rank + " of " + suit; }
    ]

  • Need to convert a binary value into decimal

    Hi i need to convert a binary value which i would be getting as a string object to a decimal value......need code for the same

    Check Integer.parseInt
    http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Integer.html#parseInt(java.lang.String,%20int)

  • Convert binary data to decimal in CAN XNET

    I am using NI-XNET Read function to read CAN frames from port CAN1 , the data out put comes in cluster in binary format is there any function that I can use to convert the binary to decimal ? I prefer not to write another sub-vi to do conversion but using the existing functions that can use my .dbc file and parse all frames in decimal format.
    Thanks.

    Yes Doug You are correct. We have a PCB and we have connected two inductive  sensors over the PCB which are measuring the distance and transpose it to voltage. The PCB has a software which transpose this voltage through an ADC and through the Can Bus we are reading the sensors .To communicate with the PCB  we are using a USB to Can device from IXXAT to communicate, we have build the Labview drivers and our problem as you mention is that we want to transpose this data to voltage. How we can do this? I try several times to upload photos or even the program we have create but for some reason the site is not letting me to do that. If you have an email i can send you the program we have create to tell us your opinion in this problem we have, we can also send you the Labview drivers and the datasheet of the sensor we are using.
    Attachments:
    Read from Can.vi ‏37 KB

  • HP3561A:Conversion Binary data in Decimal data

    Hello,
    I try to transform, with Visual BASIC 6, a variable in binary data acquired by order "DSTB" of Dynamic Analyzer HP3561A,
    in decimal data to then use it in Excel.
    Could you help me to carry out this transformation, because for the moment I pain a little. 
    Thank you in advance. 
    Cordially Xavier VACHERET

    Hello with all
    I thank you for your messages.
    After some research, here the method which I found to obtain measurements of analyzer HP3561A.
    to see the file joint 
    Perhaps that is not very academic, but that functions.
    Thank you has all. 
    Cordially
    Xavier
    Attachments:
    NI forum HP3561A_1.doc ‏36 KB

  • About binary number and decimal number?

    how to use a class to convert a decimal number from a binary number?Thank you

    You've already asked the same kind of thing before (http://forum.java.sun.com/thread.jsp?forum=31&thread=325249), and fsato4 answered. Here is the same thing, but decimal to binary.
    public class ToBinary {
         public static void main(String[] args) {
              int n = 12; 
              System.out.println(Integer.toString(n)); 
              System.out.println(Integer.toBinaryString(n));

Maybe you are looking for

  • Best way to make animation w/stills for intro?

    Kind of like the template intro movies in the themes, I want to cut up my own photo and have pieces move around and off screen. For example, I want a photo to split in half right up the middle and have each half go off screen in opposite directions.

  • Credit exposure for agent note agent is vendor ....

    Dear Friends , As per client requirement , agent is mapped as vendor through partner determination & vendor is assigned to customer master partner function . Condition records are created manually & it is statistical . Now the client requirement is ,

  • CJV4, WBS simulation version cost in COSS and COSP by cost element

    Hi PS expert.  (-: IN PS When we freeze a simulation version with CJV4,  (operative data to version), sap will create new object number in table VSCAUFV_CN for each network and VSPRPS_CN for each Wbs element related to the simulation Version. In our

  • Tools 8.40 on Solaris 10?

    Hi, I wanted to know what is the tools version that Solaris 10 machines support? Currently we are on Tools 8.40 and Solaris 8 and are now planning to upgrade to Solaris 10 (with the same tools version) and we want to know whether a tools upgrade woul

  • Latest Flash plugin causes firefox to crash when printing

    Firefox will crash anytime I attempt to print a webpage. It doesn't happen in safemode. Disabling the Adobe Flash Plugin removes the problem. The Flash Plugin is the current version from adobe.