Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Geek Culture / Binary Values (bitShifting)

Author
Message
Shadow Robert
21
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 25th Feb 2003 15:47
... i was talking Kentaree a little while back and I'm actually wondering how many people here actually understand Binary values?

well basically i'm going to just explain how bitshifting mathematics work
for this i'll explain on an 8bit Byte value

%00000000

so how does the bitshifting work?

the sum for '
Tsu'va Oni Ni Jyuuko Fiori Sei Tau!
One block follows the suit ... the whole suit of blocks is the path ... what have you found?
Hamish McHaggis
21
Years of Service
User Offline
Joined: 13th Dec 2002
Location: Modgnik Detinu
Posted: 25th Feb 2003 17:51
ummm... I think your post may have been cut off, I hate it when that happens, especially with long posts. I make a habit of copying what Ive written to the clipboard and checking to see if it has been posted.

Oh, and I dont have a clue about bit shifting so id be quite interested to see what it is, lol.

Yum! Yum! Yum! Yum!
MrTAToad
21
Years of Service
User Offline
Joined: 26th Aug 2002
Location: United Kingdom
Posted: 25th Feb 2003 17:56
Well, you'll be please to know that I do know about bitshifting (didn't come top at college & get a third at University for nothing)...

Good news everyone! I really am THAT good...
http://www.nickk.nildram.co.uk/ for great plug-ins - oh my, yes!
Killer Sponge
21
Years of Service
User Offline
Joined: 27th Nov 2002
Location: United Kingdom
Posted: 25th Feb 2003 18:03
10011001 = 1+8+16+128 = 158

Current Project: Lode Runner Db (Working Name)
Killer Sponge
21
Years of Service
User Offline
Joined: 27th Nov 2002
Location: United Kingdom
Posted: 25th Feb 2003 18:03
dunno why i posted that, guess im bored

Current Project: Lode Runner Db (Working Name)
Shadow Robert
21
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 25th Feb 2003 18:06
damn it your right
... thats the first time thats happened to me - i've posted WAAAAY longer than that before.

ack now i have to remember how i put everything

Tsu'va Oni Ni Jyuuko Fiori Sei Tau!
One block follows the suit ... the whole suit of blocks is the path ... what have you found?
rapscaLLion
21
Years of Service
User Offline
Joined: 29th Aug 2002
Location: Canada
Posted: 26th Feb 2003 00:21
lol, I was just going to ask, WTF is bitshifting

Alex Wanuch
aka rapscaLLion
Kousen Dev Progress >> Currently Working On Editors
Shadow Robert
21
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 26th Feb 2003 03:51
oki well lets try again... if it cuts it off again i'm not gonna be happy.

now we'll be using an 8bit byte value (%0000 0000)
firstly i'll go over what BitShifting does...

the name basically says what this does, you're shifting bits around in your value.

%0000 0001 << 2 and it becomes %0000 0100

basically because it takes the value you're current using and shifts all of them to the left 2 places

now %0000 0001 = 1 and %0000 0100 = 4

if you want to know why, because its likes working with arrays working with bits...

dim byte( 8,2 )

that is basically a byte array ... for each Bit you have 2 Boolean values, either 1 (ON) or 0 (OFF) which some of you may or may not already know.

so now you want to fill these values to represent real number right?

this is done like this



(i'm keeping these short to make sure they post right so see next post...)

Tsu'va Oni Ni Jyuuko Fiori Sei Tau!
One block follows the suit ... the whole suit of blocks is the path ... what have you found?
rapscaLLion
21
Years of Service
User Offline
Joined: 29th Aug 2002
Location: Canada
Posted: 26th Feb 2003 05:29
lol, sorry to inturrupt, you are obviously in the middle of the next post BUT: I'm confused because I'm tired, going to bed, and don't have time to read the thing once your done and ask you (I will 2morrow though), what can you do with it? I'm sure you'll put that in your explination but just in case

Alex Wanuch
aka rapscaLLion
Kousen Dev Progress >> Currently Working On Editors
Shadow Robert
21
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 26th Feb 2003 06:36
hopefully this is now making some more sense on what a Binary Byte actually is

now say you were to BitShift a value then what you want to so is find out the MAX value that Bit

Byte(Bit,2) = MAX_VALUE

but what exactly do you need to know the MAX value of the Bit Range?

well basically put, to use the next range or Bits you take the previous range and multiply the value to add your new value to make sure it doesn't step on the toes of the last value... probably makes no sense

however, if you take say 4 and you want to bitshift 3 and then 0 ontop of that

then the resulting value is 4 + ((2^Bit)*3) + ((2^Bit)*0)
you maybe wondering how you can actually add 0 but i'll show you

we'll be using the range 0-8 (so every 3bytes)

((2^6)*0) + ((2^3)*3) + 4

so lets break it down

( (6Bit)* 0 ) is 2^6 = 64
so 64*0 = 0
%0100 0000 * %0000 0000 = %0000 0000

( (3Bit)* 3 ) is 2^3 = 8
so 8 * 3 = 24
%0000 1000 * %0000 0011 = %0001 1000

finally we add this all up
0 + 24 + 4 = 28
%0000 0000 + %0001 1000 + %0000 0100 = %0001 1100

notice how the values don't actually change thier definition, they simply shift up

when you look at this all, it is very simplistic indeed and thinking of it as an array is one of the best ways to be able to think about howto calculate it

Tsu'va Oni Ni Jyuuko Fiori Sei Tau!
One block follows the suit ... the whole suit of blocks is the path ... what have you found?
Shadow Robert
21
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 26th Feb 2003 06:37
sorry that took so long... but i got tied up in something for a few hours

Tsu'va Oni Ni Jyuuko Fiori Sei Tau!
One block follows the suit ... the whole suit of blocks is the path ... what have you found?
rapscaLLion
21
Years of Service
User Offline
Joined: 29th Aug 2002
Location: Canada
Posted: 26th Feb 2003 23:23
I still have no clue what you would use it for...

Alex Wanuch
aka rapscaLLion
Kousen Dev Progress >> Currently Working On Editors
Shadow Robert
21
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 27th Feb 2003 02:53
lol... its simple
think of it as your own data definitions for compating data within day a byte.

i'll show you with one of the most commonly used methods
say you want to compact a 16bit colour...

%0000 0000 0000 0000

problem is a 16bit value is only 2bytes (word) which doesn't split into 3 very well

not only that but without bitshifting you can only use the bytes as singluar values... or do a similar thing to hex with 0 = black and 65536 = white and figure out a colour range between.

I'll basically use 5-5-5 setup (to prevent confusion here for now)
Red - Green - Blue
00000 00000 00000 0

2^5 = 32

so we then calculate the colour equasion -> (256/(2^5))
bitRed = Red * (256/(2^5))
bitGreen = Green * (256/(2^5))
bitBlue = Blue * (256/(2^5))

we then compile them into a single value (BGR)
wordColour = (bitBlue*(2^10)) + (bitGreen*(2^5)) + bitRed

and now we have our 16bit (32k) Colour
obviously you can achieve more with this, like creating partitioned Vectors and such - but you get the idea.

Tsu'va Oni Ni Jyuuko Fiori Sei Tau!
One block follows the suit ... the whole suit of blocks is the path ... what have you found?
rapscaLLion
21
Years of Service
User Offline
Joined: 29th Aug 2002
Location: Canada
Posted: 27th Feb 2003 04:35
yes, well I'm obviously not getting it, but I also don't get binary values and such. Try again next year when I actually know what a vector is

Alex Wanuch
aka rapscaLLion
Kousen Dev Progress >> Currently Working On Editors
Shadow Robert
21
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 27th Feb 2003 04:45
erm... how about you imagine it as beakers that can stack up and hold let say water

and you take the bottoms off and combine these beakers...

so say you have all 8 with thier bottoms you can have 8 values of either being empty (0ml) or full (1ml)

take the bottoms off every 2, and you end up with 4 beakers stacked instead of 8 ... which can now hold between 0 and 4ml

each time you take the bottoms off, you can hold more in each beaker - but they store these part independantly ... even though technically its all the same beaker

does it make any more sense now?

Tsu'va Oni Ni Jyuuko Fiori Sei Tau!
One block follows the suit ... the whole suit of blocks is the path ... what have you found?
Mr Tank
21
Years of Service
User Offline
Joined: 25th Nov 2002
Location: United Kingdom
Posted: 27th Feb 2003 17:30
dividing/multiplying by powers of 2 in binary is really easy- just move the decimal point (just loose fraction stuff if doing integer computer maths)- much like dividing/multiplying by powers of 10 in decimal etc.

I want to do something like this in my asteroids game- i`m using 32bit int values for the positions of things in my wraparound world- so i can just do subtraction/ addition and not worry about `if posx>arenasize-1 then posx=posx-arenasize: if posx<0 then posx=posx+arenasize` etc.

I then need to position things, but since the camera uses float positions, i need to divide all my positions by a large number. I`d like to just knock of the last 10 or 20 or so least significant bits. At the moment i`m just doing an int division, which i think is slower.

If you understand what i mean, can i do this stuff in db (1.13 enhanced)?! Maybe with memblocks??

Hamish McHaggis
21
Years of Service
User Offline
Joined: 13th Dec 2002
Location: Modgnik Detinu
Posted: 27th Feb 2003 19:08
A vector is a distance in a direction isn't it? Like...

Go 3 miles at a bearing of 90 degrees.

or just directions like...

Go 3 miles north and then 4 miles east.

And I understand that it isnt just to do with orienteering!

Yum! Yum! Yum! Yum!
rapscaLLion
21
Years of Service
User Offline
Joined: 29th Aug 2002
Location: Canada
Posted: 27th Feb 2003 19:20
Ok I think I'm getting it...
Is that why things in Graphics and such must be divisible by two?

Alex Wanuch
aka rapscaLLion
Kousen Dev Progress >> Currently Working On Editors
Shadow Robert
21
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 27th Feb 2003 20:57
exactly Raps

and Ed, you might've notice that when dealing with highnumbers to multiplicate with your DarkBasic will slow down big time right?

well this is simple to get around - rather than using a dword 0-4million value

what you'd do is take the world size you want to use, and the devide this into smaller values.
So let say we have a 1,000,000 (oki not greatly large, but dealing with it will be tough)

so we take this into bytesize sections
1,000,000 / 256 = 3,906.25 (can't have an float so this is dropped, and this value is still too large)

3,906 / 256 = 15.26 ... this we can handle quite nicely now

so we'll actually take this from the botton level and rank up

16 -> 4,096 -> 1,048,576

right now we use 2 tranforms for a 256 value ... this gives you 1million portals, and within each of these portals the calculations are done within byte areas of data - so rather than having 3 floats for a Vector, you can have

3 byte for a Vector ... then * by the 3 byte Tranformation ... finally * by the 3 byte Scale

which means you have
256<<16+256<<8+256<<0 * 256<<16+256<<8+256<<0 * 256<<16+256<<8+256<<0 = float position over 1million squares which in turn actually contain 256 vector points along each plane ...

this means you can do calculations local to the player on a byte scale ... and the adjust the other transform and scale values based on the grid point
(which is why you found out the range, grid, snap above)

(^_^) hope that makes some sense there Ed

Tsu'va Oni Ni Jyuuko Fiori Sei Tau!
One block follows the suit ... the whole suit of blocks is the path ... what have you found?
Mr Tank
21
Years of Service
User Offline
Joined: 25th Nov 2002
Location: United Kingdom
Posted: 27th Feb 2003 22:23
whoah just scanned through that- you seem to know what you`re talking about (i assume this from my lack of comprehension!). I`ll try to read it more slowly. Expect a load of questions when i`m finished. Cheers!

Oh yeah a couple of questions (while you`re still here)

1) I thought that multiplying together 2 int numbers would take the same amount of time no matter how big the number (eq 1 or 100000), but only dependent on the no. of bytes/bits

2) i`d like to use less than 32 bit numbers since i don`t need the accuracy, and using less bits will probably help speed, right? But i don`t think you can change the no of bits you work with in db?!

You may have already answered this- i`ll read your thing properly now.

Shadow Robert
21
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 27th Feb 2003 22:35


a problem i've noticed in DB Standard though, is that you can only declare upto 24bit values
anything over that and it goes to -max value (bloody integers) ... but there is no way to declare them as dwords rather than integers without using memblocks

infact you can't declare anything larger values without using a function or something that requires a larger value than can normally be provided through a int

see the point in using bitshifted muliplication over standard values, is that you can store data dynamically over several integer values

so like oftenly i'd make an array dim pBitValue( MAX_VALUES, bits )

you'd then store in each of them a 16bit interger (which db will happily handle) for each portion you want, which allows you to work in ANY bit value you want
you just have to be able to think about howto acess them properly (^_^)

Tsu'va Oni Ni Jyuuko Fiori Sei Tau!
One block follows the suit ... the whole suit of blocks is the path ... what have you found?
Mr Tank
21
Years of Service
User Offline
Joined: 25th Nov 2002
Location: United Kingdom
Posted: 27th Feb 2003 23:17
what i want is to be able to multiply/divide by powers of 2. This should be quicker than normal multiplication, shouldn`t it?- i mean- just reading the first n bits of 32 bit number. Your code looks like it would work, but shouldn`t be any faster- should it? (does db recognise 2^n as something special??)

My game works like this:

Everything has a position. Each dimension (my game uses 2)- the position is an integer value (ie in db from -2^31 to 2^31-1 ie 2^32 discrete values.

This means to get relative positions in my wraparound (asteroids) world, i simply take position a away from position b, and similarly, can add velocity(*timestep) to position to get new positions. This saves me having to do if statements like i mentioned already. (works like `wrapvalue`)

To display stuff i need to get the position relative to the camera. Because db drawing stuff seems to work with floats (try positioning object beyond range of float- i think this is 1*2^-12 to 2*2^12 ?!) , i need to work with smaller values- hence the need to divide positions by 2^lots. All i really want to do is read off the first n bits.

What you were saying about subdividing in to pieces and using bits in better ranges is interesting too. I should think about this. I imagine i could get a whole lot more speed out of my games if i knew what was going on <where are the smileys?!>

Hamish McHaggis
21
Years of Service
User Offline
Joined: 13th Dec 2002
Location: Modgnik Detinu
Posted: 28th Feb 2003 20:35
Hey raven, I just dont get some of the things which you are talking about, i'll list them...

1. What does this mean, how is this showing the real values of the numbers (numbers.. bits?)



2. What is a bit range?

3. Why do you want to find the MAX VALUE of a bit? Surely the max value a bit can have is 1? You find the max value of a bit by bitshifting a load of byte on top of it?

Sorry for all the questions, I just dont really get what you are doing when you combine all the values. I get how to bitshift though.

Yum! Yum! Yum! Yum!
Hamish McHaggis
21
Years of Service
User Offline
Joined: 13th Dec 2002
Location: Modgnik Detinu
Posted: 28th Feb 2003 22:12
I think I understand it now. So...



Sorry if I have anything wrong, please correct me if so, and I dont understand why you work out the colour equation Raven (red*(256/2^5)), you seem to be shifting the bits up three places, then if you shift them again, up 10 places then you lose some bits.

Thanks for your time.

Yum! Yum! Yum! Yum!
Hamish McHaggis
21
Years of Service
User Offline
Joined: 13th Dec 2002
Location: Modgnik Detinu
Posted: 28th Feb 2003 22:14
Here it is without the tags, I didnt realise they didnt work inside a code tag...



Yum! Yum! Yum! Yum!
Hamish McHaggis
21
Years of Service
User Offline
Joined: 13th Dec 2002
Location: Modgnik Detinu
Posted: 28th Feb 2003 22:16
sorry, typo on the add green value bit, should be

add green value
0 01101 00000 00000
0 00000 00010 00000 +
---------------------
0 01101 00010 00000

Yum! Yum! Yum! Yum!
Shadow Robert
21
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 4th Mar 2003 13:30
Well i'm back now and can answer questions a lil more easier now i finally have access to a computer (i did want to go online yesterday but i was determined not to break the "no computer" rule my mom said

i'll address Hamish first ...
Raven = ( Red * ( 256/2^5 ) )

is because what you're doing in compressing the binary value rather than actually shifting it
if you were using a 5-6-5 method it'd be like



so you end up there with Blue 0-255 -> 0-31 ; Green 0-255 -> 0-63 ; Red 0-255 -> 0-31

which is a 65535 16bit colour as 32 * 64 * 32 = 65536
but usually thats used with Light - Hue - Saturation Colours

(^_^) david you appear to be getting what i said quite well, rather than using real values you're using wrap values for smaller sections of space and the previous values are then added onto ... the bit ranges in case you're wondering are the sizes of the Bits

e.g.
<< 8 is a range of 0 - 255
<< 16 is a range of 0 - 65535
<< 32 is a range of 0 - 4million
etc...

basically 2^bits = Bit Range

and the reason we get the Max Bit Value for bit shifting is basically because all we're doing is adding our value to the end of this range so it doesn't overlap the original value that way when we add them all together although it makes a large number nothing has over lapped and you can simply do the maths backwards and retrieve the same value

kinda like Stack Array,
you have to use it within an order populate in one and unpopulate in the reverse

the reason that this is faster than using native Float values or larger integer values is because you're never really reaching them (^_^)

you're actually keeping the real values your working with ti 8 or 16bit which is much lighter on the processor than a 32bit integer, or even several 32bit integers trying to be multiplicated

Tsu'va Oni Ni Jyuuko Fiori Sei Tau!
One block follows the suit ... the whole suit of blocks is the path ... what have you found?
Hamish McHaggis
21
Years of Service
User Offline
Joined: 13th Dec 2002
Location: Modgnik Detinu
Posted: 4th Mar 2003 19:48
Forgive me for being stupid but, (this is probably complete nonsense) shouldnt the equation be...

redBit = RED/(256/2^5)

if you want to compress the value. It's just that multiplying increases the value of a number, therefore wouldn't it require more bits to store, and so expanding rather than compressing.

Tell me what I've misunderstood please.
Thanks

Yum! Yum! Yum! Yum!
Shadow Robert
21
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 4th Mar 2003 21:26
nope... but your right i got that wrong

redBit = Red * ( 2^5/256 )
maybe now it'll seem to make more sense

Tsu'va Oni Ni Jyuuko Fiori Sei Tau!
One block follows the suit ... the whole suit of blocks is the path ... what have you found?
Hamish McHaggis
21
Years of Service
User Offline
Joined: 13th Dec 2002
Location: Modgnik Detinu
Posted: 4th Mar 2003 21:59
Thanks, I do understand now!

Yum! Yum! Yum! Yum!
Killer Sponge
21
Years of Service
User Offline
Joined: 27th Nov 2002
Location: United Kingdom
Posted: 5th Mar 2003 14:36
just confuzled, i know what bytes are, i know binary, but nothing else makes sense perticullar your posts on who to do it, it sent my brain wacked.
??
HELP
??
K-Sponge

Current Project: Lode Runner Db (Working Name)
Shadow Robert
21
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 5th Mar 2003 15:48
what don't you understand Killer?

Tsu'va Oni Ni Jyuuko Fiori Sei Tau!
One block follows the suit ... the whole suit of blocks is the path ... what have you found?
Killer Sponge
21
Years of Service
User Offline
Joined: 27th Nov 2002
Location: United Kingdom
Posted: 5th Mar 2003 16:41
well i read through ur explanations, it makes a lil more sense this time but theres still so much missing as in sentance types, a letter a word, I dunno, just cant put ur suposed sentances together

Current Project: Lode Runner Db (Working Name)
Killer Sponge
21
Years of Service
User Offline
Joined: 27th Nov 2002
Location: United Kingdom
Posted: 5th Mar 2003 16:42
thus the explanation makes lil sense to me

Current Project: Lode Runner Db (Working Name)
Shadow Robert
21
Years of Service
User Offline
Joined: 22nd Sep 2002
Location: Hertfordshire, England
Posted: 5th Mar 2003 17:33
hmm... the sentaces are constructed fine, i don't believe i've missed words out
i don't normally

i kinda find it hard to read alot of peoples posts around here because of terrible spelling and gammar, so erm - i dunno
i can't really reword them if thats what you want because thats just my style of writing, and anything extra i write will be the same ... perhaps if you explain EXACTLY the sections you don't understand perhaps someone else can reword them for you.

Tsu'va Oni Ni Jyuuko Fiori Sei Tau!
One block follows the suit ... the whole suit of blocks is the path ... what have you found?
Hamish McHaggis
21
Years of Service
User Offline
Joined: 13th Dec 2002
Location: Modgnik Detinu
Posted: 5th Mar 2003 19:44
You were makeing quite a lot of typos Raven, I found it hard to understand what you were saying too, heres one sentence

now say you were to BitShift a value then what you want to so is find out the MAX value that Bit

It should have been I'm guessing...

now say you were to BitShift a value then what you want to do is find out the MAX value of that Bit

Not complaining though, thanks for the explanations.

Yum! Yum! Yum! Yum!
bitJericho
21
Years of Service
User Offline
Joined: 9th Oct 2002
Location: United States
Posted: 5th Mar 2003 19:57
1100101 means 'I Love You'... erm ok ok, so its a bad techno song...but who cant resist a group dedicated to computer songs, released such favorites as 'Joystick', 'Computermenschen(computerhuman)' and 'Laptop in Jamaica'?
Hamish McHaggis
21
Years of Service
User Offline
Joined: 13th Dec 2002
Location: Modgnik Detinu
Posted: 5th Mar 2003 20:48
Heres my explanation of bitshifting (you said you didn't understand anything other than binary)...

you have a byte (8 bits) and each bit in this byte has a value, as you already know...



so...

0010 0101 = 1+4+32 = 37

But you already know this so moving on quickly. To bit shift a value you basically shift all the bits either to the left or the right. If you bitshift a value right 1 space then it would be like this...

0101 0100 >>1
-----------------------
0010 1010

See, all the numbers have shifted along 1 space.
Now as you go left from the LSB (least significant bit, the one on the far right) you will see that each time the value of the bit is double the value of the last bit, so the LSB has a value of 1 and the next bit along has a value of 2 which is double 1. The next bit has a value of 4 which is double 2.
So if you bitshift the value once left then our value is going to double (times 2), as all the 1's have shifted left and therefore doubled in value. Here is an example with 4 bits (a nibble, bit-nibble-byte, makes sense)...

0101 = 1+4 = 5 <<1
1010 = 1*2+4*2 = 2+8 = 5*2 = 10

The same happens if you bitshift right, except the values are halved and therefore our overall value is divided by 2.

The problem with bitshifting is that you can loose values off of the end of the group of bits. Like this...

0101 >>1
0010

See, we lost a one off the end so the result isnt the same as 5/2 (2.5) it's 2. This is an advantage sometimes though, for example when you need to compress a value.

If we had an 8 bit colour value (up to 255 like in DB) and we wanted to compress it into a 5 bit value then you would want to get rid of the value bits, so you would shift the bits right. Lets just make the value 255 to be easy...

1111 1111 = 255 >>3
0001 1111 = 31

31 is the maximum value you can have with 5 bits, so the value is compressed but still the maximum. Now if you had 3 colour values and you wanted to compress them into a 16 bit colour, you need to bitshift them to make them all 5 bits so that they fit into 2 bytes (5*3 = 15)...

redBit = red/(256/2^5)
grnBit = grn/(256/2^5)
bluBit = blu/(256/2^5)

256/2^5 is the same as 256/32 is the same as 8 so 255/8 is the same as bishifting the value right 3 (try it and see).

Then we have these values, say the 8 bit values were 255,16 and 200...

RED 1111 1111 = 255 >>3
GRN 0000 1000 = 16 >>3
BLU 0110 0100 = 200 >>3
--------------------------
RED 0001 1111 = 31
GRN 0000 0001 = 1
BLU 0000 1100 = 12


Then we want to fit them all into 2 bytes, but we want each of the five bit values to be separate so we can read them out again easily, the zeros are just as important as the 1s, we dont want to loose them. So we bitshift the values left (we need 16 bits to do this without bits falling off the end)...

newBlue = blue*2^10
newGreen = green*2^5
newRed = red

RED 0000 0000 0001 1111
GRN 0000 0000 0000 0001 <<5
BLU 0000 0000 0000 1100 <<10

RED 0000 0000 0001 1111
GRN 0000 0000 0010 0000
BLU 0011 0000 0000 0000

or to make it look easier I'll group the bits in 5s...

RED 0 00000 00000 11111
GRN 0 00000 00001 00000
BLU 0 01100 00000 00000

Then we add the values together...

RED 0 00000 00000 11111
GRN 0 00000 00001 00000 +
BLU 0 01100 00000 00000 +
----------------------------------------
COL 0 01100 00001 11111

Now you can see that all the colour values are on there own and are compressed from 3 bytes into 2 bytes...

Now to get the values back I guess that you just bitshift them to the rightmost five bits and AND them into another byte. Here is how to and bits...

1 AND 1 = 1
1 AND 0 = 0
0 AND 1 = 0
0 AND 0 = 0

so if I and the first value out (red)...

0 01100 00001 11111
AND
0 00000 00000 11111
--------------------------
0 00000 00000 11111 = red

then Ive bitshifted the values right 5 (you can imagine that) and I and the Green value out...

0 00000 01100 00001
AND
0 00000 00000 11111
--------------------------
0 00000 00000 00001 = green

and then bitshift and AND the blue...

0 00000 00000 01100
AND
0 00000 00000 11111
--------------------------
0 00000 00000 01100 = blue

Then you have your values back...

RED 0001 1111 = 31
GRN 0000 0001 = 1
BLU 0000 1100 = 12

I hope I made sense. Hopefully with ravens and mine it will.

(Hope this doesnt cut off, Ive copied it just incase, dont know why Im writing this bit cos you wont be able to see it if it does cut off, lol!!!)

Yum! Yum! Yum! Yum!

Login to post a reply

Server time is: 2024-05-03 11:48:54
Your offset time is: 2024-05-03 11:48:54