Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (Edgar_Herrera, VoroneTZ, Akow), 973 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
confused with bits #472874
05/27/18 09:41
05/27/18 09:41
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!

Code:
void intDrawBits (int _i, int _posX, int _posY) {
	if (_i & (1 << 31)) // avoid arithmetic bitwise
		draw_text("1", _posX+=12, _posY, COLOR_WHITE);
	else
		draw_text("0", _posX+=12, _posY, COLOR_WHITE);
	int _bit = 1 << 30;
	for (; _bit>0; _bit=_bit>>1) {
		if (_i & _bit)
			draw_text("1", _posX+=12, _posY, COLOR_WHITE);
		else
			draw_text("0", _posX+=12, _posY, COLOR_WHITE);
	}
}

void intDrawBitsByBytes (int _i, int _posX, int _posY) {
	BYTE *_b = (BYTE*)&_i;
	BYTE *_bLast = _b + sizeof(int);
	for (; _b<_bLast; _b+=1) {
		BYTE _bit = 1 << 7;
		for (; _bit>0; _bit=_bit>>1) {
			if (*_b & _bit)
				draw_text("1", _posX+=12, _posY, COLOR_WHITE);
			else
				draw_text("0", _posX+=12, _posY, COLOR_WHITE);
		}
	}
}



If someone shows me these two fuctions I will state, without a doubt, that both output the same string. Both print bit by bit, from left to right, the four bytes of an integer, aren't they? They do not confused

Value:
65536 + 128 + 2 + 1
First function output:
00000000 00000001 00000000 10000011 -> as spected
Second function output:
10000011 00000000 00000001 00000000 -> WTF? The bytes are printed fine but in reverse order!

I am so sure the function is right that I got locked by my own selfconviction. Don't get me wrong, I know how to reverse the loop. The problem is that it goes against all I know about what the memory is so I need to know what is going on. I can't understand. Did I get blind?

Any idea would be apreciated. Thanks in advance.

Re: confused with bits [Re: txesmi] #472886
05/28/18 10:41
05/28/18 10:41
Joined: Oct 2007
Posts: 5,210
İstanbul, Turkey
Quad Offline
Senior Expert
Quad  Offline
Senior Expert

Joined: Oct 2007
Posts: 5,210
İstanbul, Turkey
It has to with endianness:
https://stackoverflow.com/questions/6018386/so-on-x86-64-its-big-endian/41380488#41380488

second answer on this link explains it.


3333333333
Re: confused with bits [Re: Quad] #472887
05/28/18 12:22
05/28/18 12:22
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
Thank you very much for pointing me out! So it works as it is. It explains more things. I never heard about it blush

Indeed, the reason I run over this issue is a module I am working on that let me operate with integers of undefined lenght and I sorted the bytes the same manner laugh


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