This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes how to read bit pattern? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "how to read bit pattern?" Watch "how to read bit pattern?" New topic
Author

how to read bit pattern?

manas ranjan mandal
Ranch Hand

Joined: Apr 02, 2008
Posts: 97
how to read each bit of a 32 bit integer ex 01111011001000 and convert 1 into 0?

thanks in advance
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9950
    
    6

i don't understand the question... What do you mean by 'convert 1 into 0'?

if you are trying to convert it into a decimal number, each position represents a power of 2, just like decimal each position is a power of 10.

So the rightmost digit is 2^0, the next is 2^1, the next is 2^2, etc. you just add up the values where there is a 1. so if i had a four digit binary like "0111", i'd add up 2^0 + 2^1 + 2^2, which totals 7.

Does that help?


Never ascribe to malice that which can be adequately explained by stupidity.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

If you want to convert all ones into zeros, why not just assign the value 0? That's your number with all ones converted to zeros ;)

If you also want to convert zeros into ones, there's an operator for that: ~. It returns the number with all bits flipped.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
manas ranjan mandal
Ranch Hand

Joined: Apr 02, 2008
Posts: 97
Depending upon requirement i want to create method which read each bit and convert 1 into 0 and print that 32 bit pattern with 1 converted into 0.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32689
    
    4
Sounds like a use for Rob's suggestion with the ~ operator and a method in the Integer class which creates a String with the binary pattern in.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9950
    
    6

if you convert all the 1s to 0s, and leave the 0s alone, you're left with nothing but all 0's.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: how to read bit pattern?
 
Similar Threads
. java.lang.IllegalArgumentException: callable not yet supported for native queries
Atomic operation
Reading Bits from Input Stream Stream
How useful are tag libraries?
File(s) format for SCEA -Beta