Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

JAVA binary type data

Thread view: 
Dave - 13 Apr 2007 17:27 GMT
I tried to code a small function of using bitwise operation on certain bit.
However I found JAVA does not have "binary type" as C.

It can not recognize data like 0b10101010;

If I want to use  bitwise operation  like (A | B ), is there any simple way
in JAVA to create a data similar to "0b10101010"? Thanks
Stefan Ram - 13 Apr 2007 18:07 GMT
>However I found JAVA does not have "binary type" as C.

 I am not aware of the C type you might refer to.

>It can not recognize data like 0b10101010;

 Java does not interpret this as a literal, you might use

java.lang.Integer.parseInt( "10101010", 2 )

 instead.

>If I want to use  bitwise operation  like (A | B ), is there
>any simple way in JAVA to create a data similar to
>"0b10101010"?

 Another means without a custom method is

     ( 1 << 7 )|( 0 << 6 )|( 1 << 5 )|( 0 << 4 )|
     ( 1 << 3 )|( 0 << 2 )|( 1 << 1 )|( 0 << 0 )

 or a literal with the same value, such as

     »0252«, »170«, or »0xAA«.

 A more unusual idea would be to write

   EVAL( java.lang.Integer.parseInt( "10101010", 2 ))

 and then use a preprocessor, that replaces every
 »EVAL(...)« within the source code by its value in Java.
 This might have other uses as well, and a Java editor
 would still recognize the syntax. A preprocessor could also
 transform »0b10101010«, but the source code would then not
 be recognized by Java editors anymore.
Dave - 13 Apr 2007 18:40 GMT
Thanks, your methods work.  Your second suggestion is very cute.

I think JAVA is little awkward at data type and I/O compared with C/C++.

> >However I found JAVA does not have "binary type" as C.
>
[quoted text clipped - 31 lines]
>   transform »0b10101010«, but the source code would then not
>   be recognized by Java editors anymore.
Esmond Pitt - 16 Apr 2007 05:27 GMT
> Thanks, your methods work.  Your second suggestion is very cute.
>
>  I think JAVA is little awkward at data type and I/O compared with C/C++.

As your example wasn't legal C you have no basis for comparison yet.
Chris Uppal - 13 Apr 2007 18:34 GMT
> I tried to code a small function of using bitwise operation on certain
> bit. However I found JAVA does not have "binary type" as C.
>
> It can not recognize data like 0b10101010;

There is no such syntax in either C or Java.

> If I want to use  bitwise operation  like (A | B ), is there any simple
> way in JAVA to create a data similar to "0b10101010"? Thanks

In Java (as in C) you can specify integer values in octal, decimal, or hex.
(You can also specify them as character literals in either language, but that's
not relevant here.)   Once you have an integer value -- wherever you get it,
and however you write it -- you can do bit-level operations on it in
essentially the same way as C.

The only significant differences are to do with sign extension, which, IIRC, is
mostly undefined by the C language standard, but which is explicitly defined in
Java.  The most significant difference is that >> is defined to preserve the
sign of its operand (which is not what most C programmers would expect) whereas
the >>> operator (which doesn't exist in C) does a pure bitwise left-shift.

   -- chris
Greg R. Broderick - 13 Apr 2007 22:20 GMT
> I tried to code a small function of using bitwise operation on certain
> bit. However I found JAVA does not have "binary type" as C.
[quoted text clipped - 3 lines]
> If I want to use  bitwise operation  like (A | B ), is there any simple
> way in JAVA to create a data similar to "0b10101010"? Thanks

Try "byte" and code it in hexadecimal, e.g.:

byte foo = 0xAA;

Cheers
GRB

Signature

---------------------------------------------------------------------
Greg R. Broderick            gregb+usenet200612@blackholio.dyndns.org

A. Top posters.
Q. What is the most annoying thing on Usenet?
---------------------------------------------------------------------

Danno - 14 Apr 2007 04:46 GMT
> I tried to code a small function of using bitwise operation on certain bit.
> However I found JAVA does not have "binary type" as C.
[quoted text clipped - 3 lines]
> If I want to use  bitwise operation  like (A | B ), is there any simple way
> in JAVA to create a data similar to "0b10101010"? Thanks

       int a = Integer.parseInt("1100110", 2);
       int b = Integer.parseInt("0010101", 2);
       int c = (a|b);
       System.out.println(Integer.toBinaryString(c));

Would that work for ya?
Wojtek - 16 Apr 2007 15:21 GMT
Dave wrote :
> I tried to code a small function of using bitwise operation on certain bit.
> However I found JAVA does not have "binary type" as C.
[quoted text clipped - 3 lines]
> If I want to use  bitwise operation  like (A | B ), is there any simple way
> in JAVA to create a data similar to "0b10101010"? Thanks

I always use the hexadecimal represenatation. It is easier for me to
read 0x01fa than 000111111010

Your example would be 0xaa

So:
public static final long A = 0x010f; // 0000000100001111
public static final long B = 0x1001; // 0001000000000001

the expression (A | B) will produce 0x110f or 0001000100001111

and to test for matching bits:
if ( (A & B) != 0 )
 // do something interesting

Note: I always use lower case for hex numbers. It is easier for me to
read, and the compiler does not care.

Signature

Wojtek :-)

Lew - 17 Apr 2007 00:05 GMT
> public static final long A = 0x010f; // 0000000100001111
> public static final long B = 0x1001; // 0001000000000001
[quoted text clipped - 7 lines]
> Note: I always use lower case for hex numbers. It is easier for me to
> read, and the compiler does not care.

You should be using lower case for the first letter of your variable names,
too, and for the first letter of each compound word part of your variable names.

Signature

Lew

Wojtek - 17 Apr 2007 14:26 GMT
Lew wrote :
>> public static final long A = 0x010f; // 0000000100001111
>> public static final long B = 0x1001; // 0001000000000001
[quoted text clipped - 11 lines]
> too, and for the first letter of each compound word part of your variable
> names.

Those (A and B) are constants, and by common usage they are always
upper case, not camel case.

Signature

Wojtek :-)

Lew - 17 Apr 2007 15:08 GMT
> Lew wrote :
>>> public static final long A = 0x010f; // 0000000100001111
[quoted text clipped - 5 lines]
> Those (A and B) are constants, and by common usage they are always upper
> case, not camel case.

I'm so sorry - you are absolutely correct.

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.