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.

DarkBASIC Discussion / Getting Data Bits?

Author
Message
Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 11th Jun 2009 23:55
Is there a way to Get the bits of a number? Odd question i know, but just wondering?

New Site! Check it out \/
That1Smart Guy
15
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 12th Jun 2009 00:20 Edited at: 12th Jun 2009 00:20
i think theres a bin() command that turns a number into binary, ill look up on it

edit:

its actually bin$(), its in the text section of the help files

There are only 10 kinds of people in the world, those who understand binary and those who dont
Grog Grueslayer
Valued Member
18
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 12th Jun 2009 00:21
Do you mean a yes (1) or no (0) switch or a byte 0 to 255?

That1Smart Guy
15
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 12th Jun 2009 00:22
bytes are just 8 bits so they are sorta the same

There are only 10 kinds of people in the world, those who understand binary and those who dont
Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 12th Jun 2009 01:22 Edited at: 12th Jun 2009 01:25
No A byte is different from a bit, like you said it's 8 Bits that make a byte.

I was just wondering cuz i know like for instance you can get the Low Word of a DWORD(2 words so a loword would be the first word) like this:

LOWORD = DWORD & 65535

i got that from the LOWORD() function in WinDef.h(i think) in c++ and so i thought there might be a formula to get down to the bits, oh and you can get lobyte also. it's:

LOBYTE = WORD & 0xff

I havn't looked up the Conversion for 0xFF yet(Btw 65535 is 0xffff lol)

Wait just did, it's 255, and you can get the lo bits by doing 0xf(15) also i'm pretty sure, the way this is going.

YOu could make your own bits,bytes,words,dwords probably without memblocks but idk how, cuz i looked on c++ and it's this:



or in dbc:



to make a dword, A = the first word and B = the second word. once again 0xffff is 65535 but the only problem is the << 16. i think that is a thing to make it 16 bit form or something(8 bits is a byte, 16 is a word, 32 is a dword) but you can't do that in db and without that it doesn't work(Tried it and looked at both the one i extracted from a memblock and the one i made and they didn't match up.)

I figured this would be kinda useful, you could store multiple numbers in one variable. so you could have a dword and extract the word from it and so on. or start larger and have more.

anyone know a way around the << 16 part? cuz to me that would be usefull... hmmm. you also need the << 16 for getting the HiWord of a DWORD. or << 8 for hibyte or << 1 (i'm geussing 1) for the hi bit.

New Site! Check it out \/
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 12th Jun 2009 01:29
Do you mean getting the value of specifc bits or seeing if a bit is set? & will do that:



Enjoy your day.
Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 12th Jun 2009 01:36
Well i was just talking about getting the values, just for a random thing to try and do. because if i could figure out how to do that, like i said above you could theoreticly store multiple numbers in one, and have functions to extract those numbers.(like the LOWORD and HIWORD ones in the Win32 stuff)

the only thing stopping me is the << 16 or 8 or whatever.

New Site! Check it out \/
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 12th Jun 2009 01:37
The bit shift operators << >> change the value of a number by shifting the bits left or right, there by increasing the value by multiplying it by powers of 2 to the number of shifts or by decreasing it by dividing the number by powers of 2 to the number of shifts.

so number << 16 =
number * (2 ^ 16)

Enjoy your day.
Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 12th Jun 2009 01:41 Edited at: 12th Jun 2009 02:06
OH! OK! i'm going to test this! thanks!!!!!!!!!!!!!


Ok well it works, but i can't get it to extract the hiword... here's the code:



HIWORD Returns 0, if you write the DWORD i made to a memblock using write memblock dword, then read the word from position 2 then it returns correctly so that means that this:

HIWORD = (DWORD * (2 ^ 16) ) & 65535

Is wrong. heres the hiword function from WinDef.h in c++



here's what i think it should be in dbc right?

((DWORD * (2^16)) & 65535)

Hmmmmm. other then that, it works great! lol

New Site! Check it out \/
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 12th Jun 2009 01:51 Edited at: 12th Jun 2009 01:52
Just remember, you are dealing with bit settings here. So shifting 65535 << 16 turns on all of the upper 16 bits in a 32 bit number. The 32snd bit, bit 31, determines if the number is positive or negetive in DBC, so if you look at the decimal value, it will be -65536 . But the hex value will be ffff0000 meaning the top 16 bits are set to on.

Enjoy your day.
Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 12th Jun 2009 02:15 Edited at: 12th Jun 2009 02:16
I posted before i read that so read above.


Andd i'm not doing 65535 << 16. i'm doing:

(WORD1 & 65535) | ((WORD2 & 65535) *(2^16))

or ((WORD2 & 65535) >> 16))

But does it still effect it?

But just to get it strate your saying SOMETIMES the number will return negative? that hasn't happened to me. hmmm


Have you noticed that things in dbc cooraspond to data types?

you can have 65535 images
thats a DWORD

you can have 255 Memblocks
thats a WORD

You can have 31 bitmaps
thats a BYTE

HA! sorry just realized that.

New Site! Check it out \/
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 12th Jun 2009 02:48 Edited at: 12th Jun 2009 02:57
Quote: "you can have 65535 images
thats a DWORD

you can have 255 Memblocks
thats a WORD

You can have 31 bitmaps
thats a BYTE"


That's a little mixed up.
a BYTE is 8 bits
a WORD is 2 bytes = 16 bits
a DWORD is a double WORD so it's 4 bytes = 32 bits

Quote: "HIWORD = (DWORD * (2 ^ 16) ) & 65535

Is wrong. heres the hiword function from WinDef.h in c++

((DWORD_PTR)(l)) >> 16) & 0xffff))"

You must not have understood what I wrote above:
Quote: "The bit shift operators << >> change the value of a number by shifting the bits left or right, there by increasing the value by multiplying it by powers of 2 to the number of shifts or by decreasing it by dividing the number by powers of 2 to the number of shifts"


There's a left shift << and a right shift >> . Think of left shift as multiply think of right shift as divide.

If you want the high WORD then the code should read
(DWORD / (2^16)) & 65535
Because you have to shift the bits to the right 16.

The DWORD / (2^16) is the shift. It will take the upper 16 bits of a 32 bit number and move them down into the 16 bit range (a WORD). When you & that number with 65535, it will return a value that represents the bits that are turned on in the high WORD.

In C, bit shift operations are very fast and preferred over multiplication and division when it works out in terms of powers of 2. They are mainly used to test or set groups of bits.

For example, if you wanted a fast alpha blending routine for 16 bit depth screen resolution, you could use 1 32 bit number to store 2 sets of pixel colors at a time. You would use the bit shift operators to access or set either group within the 1 number. You could cut your loops through pixels, in half.

It's not as practical in DBC to create a bit shift operator with 2^shift because that is mathematically intense.

Enjoy your day.
Grog Grueslayer
Valued Member
18
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 12th Jun 2009 02:49 Edited at: 12th Jun 2009 03:02
Quote: "Well i was just talking about getting the values, just for a random thing to try and do. because if i could figure out how to do that, like i said above you could theoreticly store multiple numbers in one, and have functions to extract those numbers.(like the LOWORD and HIWORD ones in the Win32 stuff)"


That's an interesting concept. I only see the data types as how many bytes they take up in a file and the ranges of numbers I can use them with. It would be interesting to be able to store 32 switches in only 4 bytes using BIN$(). I normally use 1 byte per switch when saving data.



Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 12th Jun 2009 03:06 Edited at: 12th Jun 2009 05:02
Quote: "That's a little mixed up.
a BYTE is 8 bits
a WORD is 2 bytes = 16 bits
a DWORD is a double WORD so it's 4 bytes = 32 bits"


Whoops sorry i meant the values. arn't those the max values for the different data types?

Quote: "There's a left shift << and a right shift >> . Think of left shift as multiply think of right shift as divide.
"


Ya i figured that out. lol whoops

Quote: "That's an interesting concept. I only see the data types as how many bytes they take up in a file and the ranges of numbers I can use them with. It would be interesting to be able to store 32 switches in only 4 bytes using BIN$(). I normally use 1 byte per switch when saving data."


Ya i see it as abunch of data you can store. so you can have the let's say BYTES of date that you need then store 2 of those in words then do that again, thats 4 sets of values then put it in a dword. thats 4 sets of data that is compressed into one variable. that's the way i see i could use this.

here's what i've got:



OH! i found the highest integer value for dbc lol....


it's:

4294967295

anything over tht registers as 0. i tried to do one step above dword(2 DWORDS) and i did << 32 (2 ^ 32) and it makes this:

4294967296

Which registers as 0 in dbc. lol


EDIT:

Hey Would this make a byte out of bits?



Because each one offsets by 1 bit(2^1 = 2, 2^2 = 4, and so on) and thats how the others were made. and if so would it be done by binary 1's and 0's for each bit?

and would this be a way to get a bit value?




Just wondering if it was possible to also

New Site! Check it out \/
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 17th Jun 2009 00:48
Quote: "OH! i found the highest integer value for dbc lol....


it's:

4294967295"


In a way that is correct but in another way that's incorrect. Integers in DBC are always signed. That means if the top bit is set, the value becomes negative. If you try and read a DWORD from a memblock, it will convert to a signed LONG which is also a DBC integer. Therefore the highest integer value is

(2^31)-1 = 2147483647

However, you can assign 4294967295 to a variable though it will be converted internally to a negative number. That means
4294967295 = -1

Enjoy your day.
Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 12th Jul 2009 06:58
Oh thats interesting thanks latch!

New Site! Check it out \/

Login to post a reply

Server time is: 2024-05-20 12:02:44
Your offset time is: 2024-05-20 12:02:44