Hello OdinXP2000
(There's some spacing in this posting, I hope it'll survive. Otherwise copy/paste and read it in notepad or something)
Homework or no homework assignment, it's a question of understanding
keep the most significant 2 bits. This can be done for images, for example. Convert 256 to 16 color depth, just keep the top bits and its a rough way to do that...
(There's some spacing in this posting, I hope it'll survive. Otherwise copy/paste and read it in notepad or something)
Homework or no homework assignment, it's a question of understanding different radixsystems. (i.e. decimal, hex, binary etc.) and that's pretty important to learn, no matter where. So please excuse this rather long post...
The way numbers usually work when we deal with the decimal system is the following:
********************
** Decimal system **
********************
We have ten symbols, digits, called 09 and any given number is made up as a sequence of these digits.
A (positive, whole) number, can be considered as a sequence of digits
[ a_n, a_n1, ... , a4,a3,a2,a1,a0 ]
Now, every digit in itself only ranges from 0 to 9 but what's important is where the digit is placed in the sequence. The decimal system assigns each digit a power of 10, where the actual value of the number can be computed as the sum of
The binary system is absolutely the same, except for the usage of powers of 2 instead of 10, and instead of digits 09 we only use 01, now simply called "bits" instead of digits.
So, that'll give you the possibility to make your computer program read and understand any given number in ANY radix, be it binary, hex or decimal.
**********************************************************
* I know that! I need to convert a number the OTHER way **
**********************************************************
The way you need to go about is using division and
modulus with the given power of the desired numbersystem.
Example: You need to write the number
123 d
in binary.
First, you'd would retrieve the lowest power by dividing the number by 2, i.e. the base of the binary number system.
123 div 2 = 61
and the modulus (the remainder of the division) would be
123 mod 2 = 1
There. That's your (least significant) digit. We'll put it in the end of the sequence... 1+

.......1 there :)
And now we proceed to handle the next bit by performing division again (this time with the quotient left by the last division)
61 div 2 = 30, 61 mod 2 = 1

we get another digit ++

......11
go again...
30 div 2 = 15, 30 mod 2 = 0

++

.....011
and so forth, until you have zero as quotient, where the algorithm stops to get the number
1111011 b
algorithm DEC>BIN 
n is given number
digitlist = [0,0,0,...,0] (just some integer list)
0>index
as long as n > 0
[
divide n by 2 and store in "q", the remainder in "r"
place "r" as a digit in the list index 0
increase index by 1
set n = q
] repeat loop

**************
** Sidenode **
**************
(Mathematically, it is not relevant how you perform divisions and such, but for a computer efficient routines can be written. Especially when dealing with binary numbers where division (by 2, 4, 8, etc.) can be replaced by BITSHIFTS. Don't bother for now.)
I hope this was of some help to you,
Anders Rasmussen.
Having to make a change to your users’ email signatures, yet again? Feel like your head is going to explode? Rely on an Exclaimer email signature management solution to make the process simple!
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…