This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Help me with byte and bits Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help me with byte and bits" Watch "Help me with byte and bits" New topic
Author

Help me with byte and bits

John Cobra
Greenhorn

Joined: Sep 06, 2011
Posts: 3
Hi all,

i have a problem... and maybe you can help

in the end i need a Byte with the "0" and "1" in the position that i want..

it means...

if (...){
set in the position x the value "1"
}

so in the begin i need a byte like 00000000

and then i start setting "1" in each position...


how can i do this??
any good soluction?

thanks a lot.
John Cobra
Greenhorn

Joined: Sep 06, 2011
Posts: 3
If i have string like

string bits = "00010010";

this will work?

byte bits = Byte.parseByte(bits , 2);

??
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11861
    
  18

I wouldn't use a string at all. I'd use a byte, a bit mask, and bit-shift operations.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
John Cobra
Greenhorn

Joined: Sep 06, 2011
Posts: 3
can you give a small example??

thanks
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11861
    
  18

I don't have time to put together formal code...beside, you'll learn more if you do it. but, in a nutshell, i'd do something like this:





 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Help me with byte and bits
 
It's not a secret anymore!