• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

how to read bit pattern?

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to read each bit of a 32 bit integer ex 01111011001000 and convert 1 into 0?

thanks in advance
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
manas ranjan mandal
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 80870
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you convert all the 1s to 0s, and leave the 0s alone, you're left with nothing but all 0's.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic