• Post Reply Bookmark Topic Watch Topic
  • New Topic

Manipulating Binary Numbers  RSS feed

 
celine mcgowan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I wonder does anybody know where i can get a class for manipulating binary numbers? I'd write it myself, but my assignment is due in 6 days & i don't really have time! Plus, it would break the rule about not re-inventing the wheel. And i'm a very good girl - i don't want to break rules!!!
Thanks a mill,
Celine
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'd write it myself, but my assignment is due in 6 days & i don't really have time!

With this attitude, you won't find much help around here. A lot of people at the Ranch are very interested in helping you learn Java, but purty much nobody cares about your grades.
So what do you mean by "manipulating binary numbers?" What is your assignment exactly?
 
celine mcgowan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ernest,
Erm, right. Sorry.
Well, what i'm doing is an applet which displays a Panel of 8 LED's. The user will be able to enter a word. Each char of the word will have to be converted to binary for display on the LED. If the bit is a one the bar is OFF if it is zero, the bar is ON:
-
| |
-
| |
-
top horizontal = Bit zero
top right vert = bit one
bottom right vert = bit two
bottom horiz = bit three
bot left vert = bit four
top right ver = bit five
middle horiz = bit six
Dot (not needed) = bit seven
So, i need a little class that will deal with the binary numbers. It might be handier if i did it with an array[8] of ints. What would you think?
Celine
 
Tom Blough
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Celine,
What you need to do is called bit masking. If you use the binary AND operator ( & ) you can determine if a bit is set in your number. For example, if you wanted to see if the bottom left vertical element was turned on, you would do the following:

The binary AND operator works by comparing each bit in the number with each bit in the mask correspondingly. Wherever the mask has a zero, the result will have a zero in that position regardless of the value in the number. Only if the number has a 1 and the mask has a 1 will the result have a one in that position. Therefore only if the number has the same bit set as the mask will the result of the AND be greater than 0.
I think your bigger problem will be choosing a data structure to hold the alphabet encoding into lit segments. I suggest using a Hashtable to map each character you want to encode, to its encoding. you should then be able to us the following to retrieve the encoding:

Where ch is the current character you are matching in the user input.
Once you get some code together and are still having problems, post what you have and we'll be happy to give you some more help.
[ November 26, 2003: Message edited by: Tom Blough ]
 
celine mcgowan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tom,
Wow, this is really helpful. Thank you very much. I'll go and try that now...
I'm not sure about hashtables as i've never used them before, but i'll try it out & if i run into any probs, i'll post again.
Thanks a million,
Celine
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!