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.

Code Snippets / some useful bit functions

Author
Message
Three Score
20
Years of Service
User Offline
Joined: 18th Jun 2004
Location: behind you
Posted: 11th Jul 2006 23:39
These are just some useful bit level functions
if your wondering "well what are these useful for, why would I want to do anything at the bit level"
well
1. some API's and such operate at the bitlevel for attributes and
2. you can store a lot of information in just 1 variable such as say you had an online game and in it you had a player TYPE that had all the info such as positions, health, and such
well in this game you had some attributes that are just rather yes or no
like "dead/respawning" and "has a weapon" and "is invincible(or some other powerup)" well instead of sending 3 variables(which are 4 bytes) you could use 1 variable(which has 32bits) and set or unset the bits accordingly instead

ok now the actual code(typed more here than in the code)


JouleOS and friends
great thanks to http://galekus.com for FREE HOSTING!
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 12th Jul 2006 02:42 Edited at: 12th Jul 2006 03:03
I planned on writing a tutorial about bit shifting to follow up on my tut about hex and binary.

Some optimizations ppl can make are:

multiply X by 2, 4, 8, 16, respectively.

X<<1
X<<2
X<<3
X<<4

Same thing for division:

X>>1
X>>2

Also, for 4 byte integers you can quickly determine the sign of negation.



"Using Unix is the computing equivalent of listening only to music by David Cassidy" - Rob Pike
Three Score
20
Years of Service
User Offline
Joined: 18th Jun 2004
Location: behind you
Posted: 12th Jul 2006 02:56
the isNegative thing is the most practical and useful, the rest is just kindof stupid to get an extra clock cycle or 2

JouleOS and friends
great thanks to http://galekus.com for FREE HOSTING!
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 12th Jul 2006 03:02 Edited at: 12th Jul 2006 03:04
that extra cycle used to be important when computers only did about 10 a second!


Your function for retrieving the bit of a value can come in handy, especially with certain file formats.

"Using Unix is the computing equivalent of listening only to music by David Cassidy" - Rob Pike
Three Score
20
Years of Service
User Offline
Joined: 18th Jun 2004
Location: behind you
Posted: 12th Jul 2006 04:19
I have made these functions in just about all of my recent dbp projects and just with all this function crap(at least 6 topics that are on the front page..) decided to post them

JouleOS and friends
great thanks to http://galekus.com for FREE HOSTING!
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 3rd Aug 2006 11:32
I came across some others I thought I'd share in the same thread.

min() and max() functions and another that determines if a value is a power of 2.



"Using Unix is the computing equivalent of listening only to music by David Cassidy" - Rob Pike

Login to post a reply

Server time is: 2024-11-23 03:19:37
Your offset time is: 2024-11-23 03:19:37