Gamestudio Links
Zorro Links
Newest Posts
loading historical data 1st time
by AndrewAMD. 04/14/23 12:54
Trade at bar open
by juanex. 04/13/23 19:43
Bug in Highpass2 filter
by rki. 04/13/23 09:54
Adding Limit Orders For IB
by scatters. 04/11/23 16:16
FisherN
by rki. 04/11/23 08:38
AUM Magazine
Latest Screens
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Hecknex World
Who's Online Now
3 registered members (AndrewAMD, The_Judge, Grant), 898 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
rki, FranzIII, indonesiae, The_Judge, storrealba
18919 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
shifting bits #404643
07/12/12 11:19
07/12/12 11:19
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline OP
Serious User
txesmi  Offline OP
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
Hi!
I'm learning how to operate with bits and trying to unify four 8 bits values into one 32 bits variable and opposite:

Click to reveal..

Code:
function main ()
{
	video_mode = 0;
	video_screen = 0;
	random_seed(0);
	
	wait(1);
	
	float fColor[4];
	unsigned int i, iColor[4], Unified;
	
	while ( !key_esc )
	{
		while ( key_any ) wait(1);
		
		Unified = 0;
		
		for ( i=0; i<4; i++ )
		{
			fColor[i] = random(1);
			iColor[i] = fColor[i] * 255;
		}
		
		printf ( "Start: %i, %i, %i, %i\n", iColor[0], iColor[1], iColor[2], iColor[3] );
		
		Unified = iColor[0];
		
		for ( i=1; i<4; i++ )
		{
			iColor[i] = iColor[i] << 8*i;
			Unified |= iColor[i];
		}
		
		printf ( "Displaced: %i, %i, %i, %i\n", iColor[0], iColor[1], iColor[2], iColor[3] );
		
		printf ( "Unified: %i\n", Unified );
		
		for ( i=1; i<4; i++ )
		{
			unsigned int Remainder = ( Unified >> 8*i ) << 8*i;
			iColor[i-1] = Unified & ~Remainder;
			Unified = Remainder;
		}
		
		iColor[3] = Unified;
		
		printf ( "Detached: %i, %i, %i, %i\n", iColor[0], iColor[1], iColor[2], iColor[3] );
		
		for ( i=1; i<4; i++ )
		{
			iColor[i] = iColor[i] >> 8*i;
		}
		
		printf ( "Final: %i, %i, %i, %i\n\n", iColor[0], iColor[1], iColor[2], iColor[3] );
		
		while ( !key_any ) wait(1);
	}
	
	sys_exit ( NULL );
}




but it looks that the first bit of the unified int variable (if i'm not wrong the sign indetifier while unsigned modifier is not supported) in last instance is not shifted to the right.



You can notify on the image that when the last value is over 128, so the unified values' first bit is 1, the last shift does not return the correct value.

I tryed with different typos but with no luck.

Could someone explain me why this happens? or how can I solve it? My knowledge in this topic is so feeble crazy

Thanks in advance.

Re: shifting bits [Re: txesmi] #404645
07/12/12 12:10
07/12/12 12:10
Joined: Dec 2008
Posts: 1,218
Germany
Rackscha Offline
Serious User
Rackscha  Offline
Serious User

Joined: Dec 2008
Posts: 1,218
Germany
The problem here seems to be arithmetic/logic shift.
You need logicshift, but maybe for rightshift, it always executes an arithmetic shift(though it HAS to use a logic shift for unsigned values :| )

For reference:
http://en.wikipedia.org/wiki/Bitwise_operation

Havent tested the compiler behaviour, yet


MY Website with news of my projects:
(for example my current
Muliplayer Bomberman,
GenesisPrecompiler for LiteC
and TileMaster, an easy to use Tile editor)
Sparetime-Development

Re: shifting bits [Re: Rackscha] #404648
07/12/12 12:49
07/12/12 12:49
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline OP
Serious User
txesmi  Offline OP
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
Thanks Rackscha, i will take a look.

I never heard about arithmetic shift frown

edited_____
ok, sure that it uses an arithmetic shift. Big pain frown

Last edited by txesmi; 07/12/12 13:08.
Re: shifting bits [Re: txesmi] #404650
07/12/12 13:02
07/12/12 13:02
Joined: Jan 2004
Posts: 3,023
The Netherlands
Helghast Offline
Expert
Helghast  Offline
Expert

Joined: Jan 2004
Posts: 3,023
The Netherlands
I've read about bitshifts before, can someone tell/explain to me what they are and do? I have no clue...

regards,


Formerly known as dennis_fantasy
Portfolio - http://www.designorhea.com/
Project - http://randomchance.cherrygames.org/
Re: shifting bits [Re: Helghast] #404652
07/12/12 13:10
07/12/12 13:10
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline OP
Serious User
txesmi  Offline OP
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
take a look to the link above. It explains it deeply.


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1