Copying 32 bits into 16 bit integer???

This is a discussion on Copying 32 bits into 16 bit integer??? within the C Programming forums, part of the General Programming Boards category; accelProps.xAxis is a 32 bit integer reading from an accelerometer... Int16U xVal = accelProps.xAxis; Will the above copy the first ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,438
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    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,185
    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, 06: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, 12: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, 02:53 PM

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