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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

integer bit values

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I'm having some trouble with some pretty basic stuff, and I was hoping someone could give me a hand. I need to write a program that asks the user to enter an integer and bit position. The program then needs to write out the value of the bit in that position of the integer (1 or 0). I know this sounds easy but I'm having trouble getting the correct values, especially in the bit position zero. I'm supposed to write it as a single method, and here's what i have so far:


Amatuerish at best I'm sure, but I'm getting correct values for most of my input. But if i input integer 7 at bit position 0, I'm getting that "Bit 0 of 7 is 0" when it should be set at 1. Also if there is a less complicated way of checking the value at a given bit position that might solve my problem as well.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
See response under duplicate post in Beginners forum...

https://coderanch.com/t/400845/java/java/bit-values-integer
 
joke time: What is brown and sticky? ... ... ... A stick! Use it to beat this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
    Bookmark Topic Watch Topic
  • New Topic