File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Binary in java - guide me

 
Manisekar Chinnasami
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i need to do logical AND mulitiplication for two binary numbers ....

100 & 111 = 100

when i do that it is working for this 3 bit .... but when i go further , it is not working ...

1111111 & 100000 = 100000 ( but the output is 33792)

here is the simple program ... find where i have done the mistake ...

class Test1
{
public static void main(String [] args)
{
int a, b=1111111,c=100000,d;
a = b & c;
d = b | c;
System.out.println("& = "+a+" | = "+d);
}
}
 
Peter van de Riet
Ranch Hand
Posts: 112
Android Chrome Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It makes perfectly sence:

decimal 1111111 is binary 100001111010001000111
decimal 100000 is binary 11000011010100000



binary 1000010000000000 = decimal 33792

Maybe you want to fill your "int a" and "int b" a different way:


The output is: & = 100000 | = 1111111
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Manisekar Chinnasami:
int a, b=1111111,c=100000,d;

You are not assigning binary numbers to the variables b and c here, but decimal numbers. Java doesn't have a way to express integer literals as binary numbers. You could try this instead:
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use Integer.toBinaryString() to get a binary representation back.
 
Manisekar Chinnasami
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually i m getting the values as integer ...

b = Integer.parseInt("1011111", 2)

here the value "1011111" is in String format ... but i m getting those values in the interger format ...

int c = 1011111;
int b = Integer.parseInt(c,2);

how can i convert int c to String ???
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

100 & 111 = 100


This part is probably done in decimal too -- and worked by dumb luck.

ie....



Henry
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

int c = 1011111;
int b = Integer.parseInt(c,2);

how can i convert int c to String ???


To repeat what everyone is saying... The value of c is assigned the decimal value of 1011111. Not the binary value of 1011111. Even if you convert it to string, using Integer.toString(), you are converting the wrong value.

Henry
 
Manisekar Chinnasami
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THEN HOW COULD I DO IT WITH THE BINARY NUMBERS ???

THE ACTUAL PROBLEMS IS,

CONSIDER A BINARY NUMBER 110110101, HAVE TO CHECK BITWISE DIGIT TO ZERO OR ONE, IF IT IS ONE, THE NUMBER LEFT UNCHANGED,ELSE IF IT IS 0, THEN IT SHOULD BE CHANGED TO ONE.

IF I USE SHIFT OPERATORS WILL IT WORK ???

IS THERE ANY WAY TO INITIALISE THE NUMBERS AS BINARY ??(i think it is not there in Java)

GIVE ME A RITE SOLUTION FOR THIS ...
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For all our eyes' sake, please KeepItDown. Writing in all uppercase is considered rude all over the net.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IS THERE ANY WAY TO INITIALISE THE NUMBERS AS BINARY ??(i think it is not there in Java)


I know this seems to be rubbing salt on the wound, but you don't seem to understand that this is more your issue, than Java's. There is no such thing as "INITIALISE THE NUMBERS AS BINARY" -- integers are in the integer format.

Decimal, Octal, Hexidecimal, are literal formats used to help the programmer specify that integer value. In your case, you want to specify a literal value -- and it is that, that you want in binary.

If there was a huge problem with mentally converting a binary (literal) value to hexidecimal or octal -- then programmers would have pushed to have binary literals added a long time ago.

Quite frankly, it takes seconds to mentally convert a binary literal value to its octal or hexidecimal counterpart. And you don't need a calculator.

110110101 --> 110 110 101 --> 0665

or

110110101 --> 1 1011 0101 --> 0x1B5


But... To answer your question. There is currently no easy way to specify a integer literal as a binary in Java. So... if you absolutely need to have it done as a binary integer literal, then you can't do it. Sorry.

Henry
 
Manisekar Chinnasami
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your reply ...

finally i have got the solution for my problem ...

i have converted it to String and performed the & operation and back to binary number and stored as integer type ...

it working ....

.....
.....
bitWiseCheck = Integer.parseInt(Integer.toBinaryString((Integer.parseInt(Integer.toString(binVal[j]),2)) & (Integer.parseInt(Integer.toString(vsVal),2))));
.....
.....
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Manisekar Chinnasami:


finally i have got the solution for my problem ...
bitWiseCheck = Integer.parseInt(Integer.toBinaryString((Integer.parseInt(Integer.toString(binVal[j]),2)) & (Integer.parseInt(Integer.toString(vsVal),2))));


I don't think so.
Try your code for -3 and -2 for instance, or 16 and 15.

I guess all you need is

That is doing a bitwise AND.

Will show you the binary representation.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic