# how to read bit pattern?

manas ranjan mandal

Ranch Hand

Posts: 97

posted 7 years ago

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?

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?

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

posted 7 years ago

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.

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 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

manas ranjan mandal

Ranch Hand

Posts: 97

posted 7 years ago

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

Posts: 49463

64

posted 7 years ago

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.