Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Example octal number = 147
- Method one: From left to right.
- Step 1: First digit is one. Take that times 8 plus 4. Got 12.
- Step 2: Take 12 times 8 + 7. Got 103, and 103 is the answer.
- Method 2, bitshift:
- Octal Number: 147.
- Step 1: 1 = 1(bin) = Shift << 3 = 1000(result value)
- Step 2: 4 = 100(bin) + 1000(result value) = 1100(result value)
- Step 3: 1100(result value) Shift << 3 = 1100000
- Step 4: 7 = 111(bin) + 1100000(result value) = 1100111
- Step 5: 1100111 binary is 103 decimal.
- for ( int i = 0; i < length; i++ ){
- c = (str.charAt(i) ^ 48);
- if ( c > 7 ) return 0;
- out = (out << 3) + c;
- }
Add Comment
Please, Sign In to add comment