JAVA binary type data

D

Dave

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
 
S

Stefan Ram

Dave said:
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.
 
C

Chris Uppal

Dave said:
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
 
D

Dave

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++.
 
G

Greg R. Broderick

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

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

byte foo = 0xAA;

Cheers
GRB

--
---------------------------------------------------------------------
Greg R. Broderick (e-mail address removed)

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

Danno

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

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?
 
E

Esmond Pitt

Dave said:
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.
 
W

Wojtek

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.

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

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.
 
L

Lew

Wojtek said:
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.

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.
 
W

Wojtek

Lew wrote :
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.

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

Lew

Wojtek said:
Lew wrote :

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top