Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Beginner: Linux vs. Dos use of << operator

Status
Not open for further replies.

weirdo

Technical User
Nov 10, 2001
13
US
I am a beginner, and appoligize for the simplicity of my question. I searched this forum, and this question appears to be unaddressed.

For simplicity's sake, condensed code is below. I am trying to reproduce the results I get in Linux when I complile and execute this code in DOS. For some reason, dos does not work well with my bitshift operand, and returns 0. The linux results, which I desire in DOS, produce 5090000.

My question is, how can I modify this code so that the windows results return 5090000? Thank you for taking the time to read this post and perhaps ponder this issue.

I am using linux's cc compiler, and Borland's Turbo C

Code:

void main()
{

int A = 5;
int B = 8;
int C = 9;

printf("(((A<<B)|C) << 16) = %X\n", (((A<<B)|C) << 16));
printf("A = %X\n", A);
printf("B = %X\n", B);
printf("C = %X\n", C);

}

Results in Linux:

(((A<<B)|C) << 16) = 5090000
A = 5
B = 8
C = 9

Results in DOS:

(((A<<B)|C) << 16) = 0
A = 5
B = 8
C = 9
 
16-bit integer Turbo C? Of course, int (x<<16 ) is equal to 0...
 
Dear Ark,

Thank you for answering my post. It is my understanding that this code snippet takes the integer 5, then shifts it 8 bits leftwise, to create 500, then puts a 9 on the end, creating 509, then shifts that 16 bits leftwise, making everything end up "5090000". This is the result in linux.

However, windows ends up with 0, using the exact same code :(. My question is, how can I modify this code so that it ends up 5090000?

Thanks,
weirdo
 
If you have 16-bit platform compiler (Turbo C is 16-bit), you have 16 bits in ints (print sizeof(int), you will get 2 (bytes in that case) in Turbo C and 4 (probably) on Linux, or 8 on 64-bit platform etc). So you can't do left shift of 16-bit word with 16 and save the result (all bits are shifted;). Do you understand this fact? For example, max int value of int in Turbo C is 32767, not a 5090000!

Change int to long (32 bit in Turbo C). This code works fine on 16 and 32 bit platforms (but slower in Turbo C).

Remember: word size is not defined by the language, so integer shifts are (partially) platform dependent ops.
 
If you are going multi-platform, keep to short or long. int is just the most convenient word size and varies from platform to platform.
 
Dear sirs,

It appears that initalizing the variables as long, rather than int, creates the same problem - the same result when the code is executed.

I should note that I am running WinME.

weirdo
 
Are you running the exe file in a dos box or what?

Not that that should make any difference...

Do you mean that you've tried declaring your variables as

long A = 5L; etc and it still doesn't work?
 
Not initalizing the variables as long but declare all variables as long!
 
You also need to change your printf() format characters to match your new data types (expecially if you've made things long).

Oh, and as always, main returns an int, not void.


--
 
Dear people,

Thank you for your very helpful suggestions. I will try these solutions and I am sure they will work.

Weirdo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top