diku and huge numbers....
Goto page 1, 2  Next
 
Post new topic   Reply to topic    mudlab.org Forum Index -> Coding
View previous topic :: View next topic  
Author Message
Kyuss



Joined: 13 May 2005
Posts: 37
Location: Southern Hellinois

PostPosted: Mon May 15, 2006 9:37 pm    Post subject: diku and huge numbers.... Reply with quote

traditional diku, is limited in scope by bitvector.


/* For players : specials.act */
#define PLR_BRIEF 1
#define PLR_NOSHOUT 2
#define PLR_COMPACT 4
#define PLR_DONTSET 8 /* Dont EVER set */
#define PLR_AUCTION 16
#define PLR_GOSSIP 32
#define PLR_NOTELL 64
#define PLR_ECHO 128
#define PLR_NOBEEP 256
#define PLR_WIZLOG 512
#define PLR_SHOWEXITS 1024
#define PLR_HOLYLITE 2048
#define PLR_QUEST 4096
#define PLR_GAG 8192
#define PLR_COLOR 16384
#define PLR_BERSERK 32768
#define PLR_PARRY 65536
#define PLR_MAILING 131072
#define PLR_WRITING 262144
#define PLR_RECEIVE 524288
#define PLR_EVADE 1048576
#define PLR_REPORT 2097152
#define PLR_BLOCK 4194304
#define PLR_SPIRIT 8388608
#define PLR_AUTOLOOT 16777216
#define PLR_AUTOSAC 33554432
#define PLR_AUTOSPLIT 67108864
#define PLR_SPAM 134217728
#define PLR_SPAM2 268435456
#define PLR_MATRIX 536870912
#define PLR_RED_REP 1073741824

to go beyond 2147483648 needs to support long long intergers

is there an easy way to workaround this inability for diku to
use numbers which go beyond 2.14billion?

assigning something as:

int j = 0; /* works */
unsigned int k = 0; /* works */
double int k = 1; /* works */
long int h = 2; /* works */
long long int tyr = 0; /* wont work */

most folks would say why go beyond, mainly because I need too, in a
couple of special circumstances.

this is booted under cygwin, which I assume can handle large large
numbers, so how would I incorporate this feature into the mud itself?

thanks...

~K
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
Author Message
Kjartan



Joined: 13 May 2005
Posts: 110

PostPosted: Tue May 16, 2006 12:32 am    Post subject: Reply with quote

You will have to go to an array of ints. Also, you will have to stop representing the constants as the actual mask and represent them as regular integers 0, 1, 2, 3, 4, ...

Say that the array is declared
Code:
   char flags[ 10 ];


Then to set a bit, it would be
Code:
   void set_bit( int bit, char *flags ) {
      flags[ bit >> 3 ] |= ( 1 << ( bit - ( ( bit >> 3 ) << 3 ) ) );
   }

To unset it,
Code:
   void clear_bit( int bit, char *flags ) {
      flags[ bit >> 3 ] &= ( 255 - ( 1 << ( bit - ( ( bit >> 3 ) << 3 ) ) ) );
   }

To check it,
Code:
   char get_bit( int bit, char *flags ) {
      return ( flags[ bit >> 3 ] &  ( 1 << ( bit - ( ( bit >> 3 ) << 3 ) ) ) );
   }

Anyway that's approximately right, you may have to do some casting to get it to compile properly.
Back to top
View user's profile Send private message Visit poster's website
Author Message
Kjartan



Joined: 13 May 2005
Posts: 110

PostPosted: Tue May 16, 2006 12:38 am    Post subject: Reply with quote

Ah, also you can replace

Code:
( bit - ( ( bit >> 3 ) << 3 ) ) )


with

Code:
( bit & 7 )


for clearer code.
Back to top
View user's profile Send private message Visit poster's website
Author Message
Kjartan



Joined: 13 May 2005
Posts: 110

PostPosted: Tue May 16, 2006 12:39 am    Post subject: Reply with quote

And one more thing ... probably get_bit should test for whether that expression is != 0, not just return it - then you'll get 0 for unset and 1 for set instead of 0 for unset and something not zero for set.
Back to top
View user's profile Send private message Visit poster's website
Author Message
Kyuss



Joined: 13 May 2005
Posts: 37
Location: Southern Hellinois

PostPosted: Tue May 16, 2006 1:01 am    Post subject: Reply with quote

proof that you are much smarter then me.

I will spend the next few days or so digesting this post Smile
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
Author Message
Kjartan



Joined: 13 May 2005
Posts: 110

PostPosted: Tue May 16, 2006 2:33 am    Post subject: Reply with quote

*blushes*

but anyway I am still wrong, it's not bit & 7, it's bit & 255.
Back to top
View user's profile Send private message Visit poster's website
Author Message
eiz



Joined: 11 May 2005
Posts: 152
Location: Florida

PostPosted: Tue May 16, 2006 12:59 pm    Post subject: Reply with quote

Kjartan wrote:
*blushes*

but anyway I am still wrong, it's not bit & 7, it's bit & 255.


??? bit & 7 is correct if you're addressing bytes (as you want the 3 low order bits: 1+2+4)
Back to top
View user's profile Send private message Visit poster's website
Author Message
Kjartan



Joined: 13 May 2005
Posts: 110

PostPosted: Wed May 17, 2006 7:18 pm    Post subject: Reply with quote

Ah yes eiz is right. I confused myself there and forgot the 1 << out front.
Back to top
View user's profile Send private message Visit poster's website
Author Message
Kaz



Joined: 05 Jun 2005
Posts: 24
Location: Hampshire, UK

PostPosted: Thu May 18, 2006 11:55 am    Post subject: Reply with quote

Bitsets are WAY easier than that. All those 3s and 255s... All you need to know is how many bits are in a byte, and you're off!

Code:

#include <limits.h>
#include <stdio.h>

void set_bit(unsigned char* bitset, unsigned int index)
{
    bitset[index / CHAR_BIT] |= (1 << (index % CHAR_BIT));
}

void clear_bit(unsigned char* bitset, unsigned int index)
{
    bitset[index / CHAR_BIT] &= ~(1 << (index % CHAR_BIT));
}

int bit_is_set(unsigned char* bitset, unsigned int index)
{
    return (bitset[index / CHAR_BIT] & (1 << (index % CHAR_BIT))) != 0;
}

int main()
{
    unsigned char bitset[2] = {0, 0};
    unsigned int i;

    set_bit(bitset, 2);
    set_bit(bitset, 8);
    set_bit(bitset, 9);
    set_bit(bitset, 12);

    clear_bit(bitset, 9);

    for(i = 0; i < CHAR_BIT * 2; ++i)
    {
        printf("bit %d = %s\n", i, (bit_is_set(bitset, i) ? "Yes" : "No"));
    }
}


Code to create bitsets large enough for a given amount of bits, toggling bits, comparing bitsets, etc., is left as an exercise to the reader.
Back to top
View user's profile Send private message
Author Message
Kjartan



Joined: 13 May 2005
Posts: 110

PostPosted: Thu May 18, 2006 4:01 pm    Post subject: Reply with quote

This code seems essentially identical to mine, except you have replaced my bitwise ops and shifts with mods and integer divisions. Certainly the data storage format (the "bitset") is identical to what I was using.

I used the shifts and bitwise ops from habit, because many years ago speed was important. I'm sure with a modern computer your method is plenty fast enough.
Back to top
View user's profile Send private message Visit poster's website
Author Message
eiz



Joined: 11 May 2005
Posts: 152
Location: Florida

PostPosted: Thu May 18, 2006 6:48 pm    Post subject: Reply with quote

Kjartan wrote:
This code seems essentially identical to mine, except you have replaced my bitwise ops and shifts with mods and integer divisions. Certainly the data storage format (the "bitset") is identical to what I was using.

I used the shifts and bitwise ops from habit, because many years ago speed was important. I'm sure with a modern computer your method is plenty fast enough.


Any modern compiler can use strength reduction to transform them into equivalent code anyway. I use the bitwise operations because bit twiddling is hardwired into my brain. Too much time spent dealing with arcane file formats and writing assemblers. Smile

Real programmers use bit ops.
Back to top
View user's profile Send private message Visit poster's website
Author Message
Kjartan



Joined: 13 May 2005
Posts: 110

PostPosted: Thu May 18, 2006 9:28 pm    Post subject: Reply with quote

eiz wrote:
Any modern compiler can use strength reduction to transform them into equivalent code anyway.


It depends what CHAR_BIT is; if it's #define'd, yes, if it's "int CHAR_BIT = 8", then I think no.

I love that chunk of example code you linked to. Now that's some bit twiddling right there.

(And actually I don't know what "strength reduction" is, I'm assuming you just mean that the compiler will transform them into equivalent code. I looked it up on wikipedia and their definition didn't seem relevant, or if it is it's in some manner too subtle for me.)


Last edited by Kjartan on Thu May 18, 2006 11:22 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
Author Message
KaVir



Joined: 11 May 2005
Posts: 565
Location: Munich

PostPosted: Thu May 18, 2006 11:10 pm    Post subject: Reply with quote

Kjartan wrote:
It depends what CHAR_BIT is


CHAR_BIT is defined in limits.h.

Kaz's usage of 'unsigned char' is also preferable to your usage of 'int', for portability reasons (an unsigned char never has padding bits, nor do you have to worry about nasty sign bit surprises).
Back to top
View user's profile Send private message Visit poster's website
Author Message
Kjartan



Joined: 13 May 2005
Posts: 110

PostPosted: Thu May 18, 2006 11:21 pm    Post subject: Reply with quote

I believe his 'unsigned char' maps to my 'char' and his 'unsigned int' maps to my 'int'; otherwise no doubt you are right. I wrote that code off the top of my head - it may well contain other errors as well.
Back to top
View user's profile Send private message Visit poster's website
Author Message
KaVir



Joined: 11 May 2005
Posts: 565
Location: Munich

PostPosted: Fri May 19, 2006 7:13 am    Post subject: Reply with quote

Ah yes, sorry, I misread your example code. However 'unsigned char' is still preferable to 'char' for the same reasons.

The usage of 'char' (or even if you'd been using 'int') isn't an error, it's just not as portable.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    mudlab.org Forum Index -> Coding All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2002 phpBB Group
BBTech Template by © 2003-04 MDesign