Jump to content
tom 2007

Bitwise "or" Bug?

Recommended Posts

Hello,

 

Today I noticed some (what I think) strange behavior of the | operator:

#define TEST_1 1
#define TEST_2 2
#define TEST_4 4
#define TEST_8 8
#define TEST_16 16
#define TEST_32 32
#define TEST_64 64
#define TEST_128 128
#define TEST_256 256
#define TEST_512 512
#define TEST_1024 1024
#define TEST_2048 2048
#define TEST_4096 4096
#define TEST_8192 8192


void main(){
	unsigned int test;
	unsigned int test2;
	
	test = TEST_1 | TEST_2 | TEST_4 | TEST_8 | TEST_16 | TEST_32 | TEST_64 | TEST_128 | TEST_256 | TEST_512 | TEST_1024 | TEST_2048 | TEST_4096 | TEST_8192;
	test2 = TEST_1 + TEST_2 + TEST_4 + TEST_8 + TEST_16 + TEST_32 + TEST_64 + TEST_128 + TEST_256 + TEST_512 + TEST_1024 + TEST_2048 + TEST_4096 + TEST_8192;
	
	//test = 8447
	//test2 = 16383
	if (test == test2){ 
		//...
		
	}
}

when splitting the test = TEST1 ... line to:

	test = TEST_1 | TEST_2 | TEST_4 | TEST_8 | TEST_16 | TEST_32;
	test = test | TEST_64 | TEST_128 | TEST_256 | TEST_512;
	test = test | TEST_1024 | TEST_2048 | TEST_4096 | TEST_8192;
	

then test becomes equal to 16383 ...

 

Compiler: boostc 7.12 (same problem under 7.10)

 

Share this post


Link to post
Share on other sites

Tom,

 

I can also confirm that the problem exists.

 

Replacing the defines directly with the values also fails as does various placements of parenthesis and casting to try and break up the problem.

 

The code seems to break with your values as soon as you have more than 8 OR'ed above 256.

 

Interestingly if you use enum {} for the values instead of your #defines your example works correctly and as a bonus the code generated is shorter too!

 

Must be some clues there for Pavel and Dave.

 

Regards

 

davidb

Share this post


Link to post
Share on other sites

Mmm, another clue - If you reverse the order of your values so that the highest occurs first then the example again works.

 

Regards

 

davidb

Share this post


Link to post
Share on other sites

Yes I've noticed some of those things too. It took a while before I found the problem was in the compiler and not my code :D In the assembler it looks like this:

 

SETF main_1_test

MOVLW 0x20

MOVWF main_1_test+D'1'

SETF main_1_test2 ;0x20FF = 8447 is loaded into test directly

MOVLW 0x3F

MOVWF main_1_test2+D'1'

MOVF main_1_test2, W

CPFSEQ main_1_test

BRA label1

MOVF main_1_test2+D'1', W

CPFSEQ main_1_test+D'1'

Edited by tom 2007

Share this post


Link to post
Share on other sites

Your content will need to be approved by a moderator

Guest
You are commenting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×