Thread: Registers

  1. #1
    Registered User
    Join Date
    Sep 2001

    Question Registers

    I have a 16 bit register (0-15). I want to store a 10 bit number in bits 5-14 as bits 0-4 are reserved. When I use the left shift operator the number multiplies by 32, therefore when I read the register I have stored a different value. Is there a way around this?

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    is this a hardware register or a defined by you register. If the latter you can define it as a bit field then you will be able to do what you want with the bits you want to play with and leave the reserved bits alone.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :-

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    If you want to preserve the bits already there, then you need to do a read/modify/write type operation.

    int temp = reg;   // read your register
    temp = temp & 0x1F;  // mask off all the bits you want to change
    temp = temp | (new_value << 5); // add your new bits, shifted into the correct place
    reg = temp;  // assign back to your reg.
    > If the latter you can define it as a bit field
    If its an internal data structure, then this is OK, but it's far from portable if its an external register/data structure.
    The layout of bit fields is compiler specific.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing HW registers using bit fields and UINT32
    By samdomville in forum C Programming
    Replies: 4
    Last Post: 12-10-2008, 01:00 PM
  2. Replies: 10
    Last Post: 12-05-2008, 12:47 PM
  3. Using MMX & XMM Registers
    By HyperShadow in forum C++ Programming
    Replies: 3
    Last Post: 07-14-2007, 12:53 AM
  4. memcpy with 128 bit registers
    By grady in forum Linux Programming
    Replies: 2
    Last Post: 01-19-2004, 06:25 AM
  5. RAM vs Cache & Registers
    By Grins2Pain in forum C++ Programming
    Replies: 3
    Last Post: 09-26-2003, 09:02 AM