This week's book giveaway is in the Testing forum.We're giving away four copies of The Way of the Web Tester: A Beginner's Guide to Automating Tests and have Jonathan Rasmusson on-line!See this thread for details.
Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# how to?

Brett Knapik
Ranch Hand
Posts: 255
How do I determine the binary code (0's and 1's) of a number?
------------------
I wish there was a button on my monitor to turn up the intellegince.
Theres a button called 'brightness' but it doesn't work

Tony Alicea
Sheriff
Posts: 3226
5
int i = 14;
System.out.println(Integer.toString(i,2));

Brian Snyder
Ranch Hand
Posts: 142
or the number 1011 =
1(1) + 1(2) + 0(4) + 1(8)= 11 (read 1011 from right to left)

Randall Twede
Ranch Hand
Posts: 4489
3
and if that doesnt confuse you, for a "twos complement" negative number, take the positive binary number, reverse all the 0's and 1's then add 1.
1 = 0001
-1 = 1110+1 = 1111

Brett Knapik
Ranch Hand
Posts: 255
I meant without using java code.
------------------
I wish there was a button on my monitor to turn up the intellegince.
Theres a button called 'brightness' but it doesn't work

Grant Crofton
Ranch Hand
Posts: 154
Calculators are always a good bet.

Asuthosh Borikar
Ranch Hand
Posts: 75
Start with dividing the number by two. 14/2 = 0, and the dividend(I am pretty certain this is the wrong word) is 7. Divide 7 by 2 again. The remainder is 1 and the dividend is 3. Divide 3 by 2. Remainder is 1 and dividend is 1. Since 1<2, we stop the division here. Now, write the remainders from right to left.
In this case, the binary value of 14 would be:1110.
You can apply this process to any number, to get its binary equivalent.
But, as Grant has suggested, calculators are always a good bet.

Brett Knapik
Ranch Hand
Posts: 255
Ok, great thanks, anymore tips are welcome
------------------
I wish there was a button on my monitor to turn up the intellegince.
Theres a button called 'brightness' but it doesn't work

Brett Knapik
Ranch Hand
Posts: 255
hmm... I think I missed something here. I am trying 86 so
86/2=43
43/2=21 r1
21/2=10.5 ??
10.5/2=5.25 ??
5.25/2=2.625 ??
2.625/2=1.3125 ??
would the answer be 011111? I doubt it there must have been some rules left out or a misunderstanding.
------------------
I wish there was a button on my monitor to turn up the intellegince.
Theres a button called 'brightness' but it doesn't work
[This message has been edited by Brett Knapik (edited February 06, 2001).]

Matthew Jones
Ranch Hand
Posts: 68
Here's how I do it. I write out the decimal values corresponding to each binary digit:
128 64 32 16 8 4 2 1
Then I take my number, and find the biggest one that would fit, in the example, it was 86, so ti put a 1 under 64:
128 64 32 16 8 4 2 1
0 1
Then subtract 64 from 86, leaving 22, take 22, and put a 1 under 16:
128 64 32 16 8 4 2 1
0 1 0 1
Leaving 6, repeat the process, and you get:
128 64 32 16 8 4 2 1
0 1 0 1 0 1 1 0
And that's the binary for 86.
There are probably faster ways of doing it, but I think this is the easiest.
Well, I can't get the numbers to line up right, but I think you get the idea.
[This message has been edited by Matthew Jones (edited February 06, 2001).]

Brett Knapik
Ranch Hand
Posts: 255
according to my test program the answer should be 1010110 here is the source code.
I used Tony's way of getting the answer. One of the ways are wrong, which one could it be?

------------------
I wish there was a button on my monitor to turn up the intellegince.
Theres a button called 'brightness' but it doesn't work
[This message has been edited by Brett Knapik (edited February 06, 2001).]

Steve Fahlbusch
Bartender
Posts: 605
7
Brett,
You are almost there, just a little more formaility. What you want to is iterate through every power of 2 from the largest power that fits down to 1 (2 to the 0 power).
So for 86 (the largest power of two is 6 or 64)
does 64 fit into 86 (yes) so you have a 1 {now subtract 64 from 86}
does 32 fit into 22 (no 32 > 22 ) so you have a 0
does 16 fit into 22 (yes) you have a 1 (subtract 16 from 22)
does 8 fit into 6 (no) you have a 0
does 4 fit into 6 (yes) you have a 1 (subtract 4 from 6 )
does 2 fit into 2 (yes) you have a 1 (subtract 2 from 2)
does 1 fit into 0 (no) you have a 0
were done with 2 to the 0 so we are done (now concatinate the 0's and 1's
1010110
or 64 + 16 + 4 + 2 = 86

Brett Knapik
Ranch Hand
Posts: 255
ok so i'll try the number 72.
72-64 = 8
8-8 = 0
so the answer would be 1001000. ok just one more quick question is there a trick for figuring out negetives and what is this sign bit thing, how can i distinguish that.
------------------
I wish there was a button on my monitor to turn up the intellegince.
Theres a button called 'brightness' but it doesn't work

Peter Tran
Bartender
Posts: 783
Brett,
1. 86 / 2 = 43 R = 86 % 2 = 0
2. 43 / 2 = 21 R = 42 % 2 = 1
3. 21 / 2 = 10 R = 21 % 2 = 1
4. 10 / 2 = 5 R = 10 % 2 = 0
5. 5 / 2 = 2 R = 5 % 2 = 1
6. 2 / 2 = 1 R = 2 % 2 = 0
7. 1 / 2 = 0 R = 1 % 2 = 1
Writing this answer from #7 to #1 from you get: 1010110.
With 72, you do the same thing.
1. 72 / 2 = 36 R = 72 % 2 = 0
2. 36 / 2 = 18 R = 36 % 2 = 0
3. 18 / 2 = 9 R = 18 % 2 = 0
4. 9 / 2 = 4 R = 9 % 2 = 1
5. 4 / 2 = 2 R = 4 % 2 = 0
6. 2 / 2 = 1 R = 2 % 2 = 0
7. 1 / 2 = 0 R = 1 % 2 = 1
Answer from #7 to #1 is: 1001000.
-Peter

Brett Knapik
Ranch Hand
Posts: 255
I find steve's way easier and the answer matches yours peter
------------------
I wish there was a button on my monitor to turn up the intellegince.
Theres a button called 'brightness' but it doesn't work

Narendra Joshi
Greenhorn
Posts: 12
Peter has given the text book method which is 100% correct, we can do it other way also manually if the number is small enough.

Peter Tran
Bartender
Posts: 783
Brett,
To the other part of your question, if you want the negative of a number then get the binary value of the positive value. Negate all bits and add 1. NOTE, carry out any leading 0 to your sign bit. Assuming a byte (8 bits) below.
E.g. From above example, 86 is 01010110. Note my sign bit (position #8 is 0 for positive sign).
1. Get binary result for positive value. 86 -> 01010110.
1. Negate all bits: 10101001.
2. Add 1 -> 10101010 = -86.
Note, since I'm assuming a byte which has 8 bits, my sign bit (bit #8) is now 1.
-Peter