Gamestudio Links
Zorro Links
Newest Posts
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
Data from CSV not parsed correctly
by EternallyCurious. 04/20/24 21:39
M1 Oversampling
by 11honza11. 04/20/24 20:57
Scripts not found
by juergen_wue. 04/20/24 18:51
zorro 64bit command line support
by 7th_zorro. 04/20/24 10:06
StartWeek not working as it should
by jcl. 04/20/24 08:38
folder management functions
by VoroneTZ. 04/17/24 06:52
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (rki), 405 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, howardR, 11honza11, ccorrea, sakolin
19047 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