Registers

This is a discussion on Registers within the C Programming forums, part of the General Programming Boards category; I have a 16 bit register (0-15). I want to store a 10 bit number in bits 5-14 as bits ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    8

    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
    Posts
    2,572
    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 :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

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

    Code:
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 12:00 PM
  2. Replies: 10
    Last Post: 12-05-2008, 11:47 AM
  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, 05:25 AM
  5. RAM vs Cache & Registers
    By Grins2Pain in forum C++ Programming
    Replies: 3
    Last Post: 09-26-2003, 09:02 AM

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