We help IT Professionals succeed at work.

can java implement bit-fields as in C?

eugeneng
eugeneng asked
on
in c, we can use the bit field as below :

     struct Variable
     {
          unsigned NameOffset : 24;
          unsigned Type : 8;
          int Value;
     };

can java do that too?
Comment
Watch Question

Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
no.
But it does have bit shift operator similiar to C.
No, You cant set the bit value.

Author

Commented:
objects,
   doesn't it  got >> & << in java ?

and ksivananth,
    what about this:

 int a,b;
     b = 31;
     a = b&16;

can't these be done in java ?

but I'm only concern about the bit field setting in order to align the struct
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Yes <<, >> are the bit shift operators I mentioned above.
And yes you can set specific bit using these & operator.

But as far as bitfield's in structs Java has no support for this.

Author

Commented:
ok than, thanx alot man. Any other way can implement something similar to bit field  as in C?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Not really, I think Java tries to avoid things like that.

Author

Commented:
ok, that a lot man, you really offered great help.

Author

Commented:
ok, thanx a lot man, you really offered great help to me.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
No worries :)
Thanks for the points.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.