Thread: Copying 32 bits into 16 bit integer???

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    17

    Copying 32 bits into 16 bit integer???

    accelProps.xAxis is a 32 bit integer reading from an accelerometer...

    Int16U xVal = accelProps.xAxis;

    Will the above copy the first 16 bits of the 32 bit integer into the 16 bit integer?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If by 'first', you mean 'least significant', then yes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    17
    Thanks!!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Isn't this implementation defined, however? Truncation?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Elysia View Post
    Isn't this implementation defined, however? Truncation?
    Don't have my copy of the standard with me, but I'm fairly certain that it specifies modular arithmetic for a down-conversion like this (which is the same as throwing away the most significant bits).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting 32 bit binary IP to decimal IP (vice-versa)
    By Mankthetank19 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 07:17 PM
  2. Reading in 16 and 24-bit audio data into (32-bit) integer buffers
    By theblindwatchma in forum C Programming
    Replies: 2
    Last Post: 04-13-2008, 11:12 PM
  3. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  4. Replies: 7
    Last Post: 08-19-2007, 08:10 AM
  5. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM