C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-26-2008, 03:46 AM   #1
Registered User
 
Join Date: Nov 2008
Posts: 8
Help with cast in C!

Can somebody help me with the cast in C?!
I wanna cast 32bit integer into 8bit char value, and the problem is that I don't know which 8bits of the integer would be taken and stored in the 8bit char variable?! The most significant or least significant?!!
Tirania is offline   Reply With Quote
Old 11-26-2008, 03:52 AM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
If you use a plain cast, then the lowest 8 bits are being used. This is probably the answer you wanted.

Slightly more complex situation:
If you cast a pointer to another pointer type, and then dereference it, then it's the 8 bits with the lowest address you get. Depending on the byte-order of the machine, you get EITHER the lowest or the highest.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 11-26-2008, 03:52 AM   #3
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,365
Quote:
Originally Posted by Tirania
I wanna cast 32bit integer into 8bit char value, and the problem is that I don't know which 8bits of the integer would be taken and stored in the 8bit char variable?! The most significant or least significant?!!
According to the C Standard, "if the destination type is signed, the value is unchanged if it can be represented in the destination type (and bit-field width); otherwise, the value is implementation-defined."

Frankly, it would be clearer to use bitwise operations to extract those bits that you want.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 11-26-2008, 03:53 AM   #4
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Quote:
Originally Posted by laserlight View Post
According to the C Standard, "if the destination type is signed, the value is unchanged if it can be represented in the destination type (and bit-field width); otherwise, the value is implementation-defined."

Frankly, it would be clearer to use bitwise operations to extract those bits that you want.
Ah, good point - it is undefined, so the compiler is allowed to do whatever it likes with it. However, I have not yet "met" a compiler that doesn't do what I explained above.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 11-26-2008, 04:02 AM   #5
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,365
Quote:
Originally Posted by matsp
Ah, good point - it is undefined, so the compiler is allowed to do whatever it likes with it. However, I have not yet "met" a compiler that doesn't do what I explained above.
I had in mind an iterative bitwise copy of the 8 bits in question, but come to think of it, the implementation defined behaviour can be avoided with just one bitwise and, e.g.,
Code:
char c = (char)(i & 0xff); // where i is the source integer
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Reply

Tags
c code, cast, char, integer

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Including The Right DLLs bumfluff Game Programming 8 12-28-2006 03:32 AM
How to fix misaligned assignment statements in the source code? biggyK C++ Programming 28 07-16-2006 11:35 PM
Converting Double to Float thetinman C++ Programming 7 06-17-2006 02:46 PM
errors in class(urgent ) ayesha C++ Programming 1 11-10-2001 10:14 PM
errors in class(urgent) ayesha C++ Programming 2 11-10-2001 06:51 PM


All times are GMT -6. The time now is 08:06 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22