Shifting Problem

This is a discussion on Shifting Problem within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to add some value to the low byte of a number according to my logic this ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    3

    Shifting Problem

    Hi,

    I am trying to add some value to the low byte of a number according to my logic this should work.

    Current code should work assuming number is unsigned.
    Code:
    nValue = nValue | ( (nValue & 0xFF) | nAdd);
    it doesn't work with many numbers,even though its simply should 100 %.

  2. #2
    Epy
    Epy is offline
    Hen-teaser Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    880
    Try

    Code:
    nValue = (nValue & 0xFFFFFF00) | ((nValue | nAdd) & 0xFF);

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    26
    Quote Originally Posted by Mr.Generic
    nValue = nValue | ( (nValue & 0xFF) | nAdd);
    It doesn't work because the logic is flawed. Try working out the result in your mind when both nValue and nAdd are 1. The result should be 0b10 (decimal 2), but your code produces just 0b1 (decimal 1). You also don't take 'carry' into consideration at all - when corresponding bits are both 1, the sum of the bits should be 0 and a carry of 1 for the next higher bit should be produced. (Hint: Look into how half-adder & full-adder are implemented; also there are Google hits containing the solution.)
    Last edited by qwertylurker; 08-17-2010 at 05:45 AM.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    3
    thanks both for your answer.
    I didn't google because I didn't know what exactly to google for :P,but anyway I get why it doesn't work now,also thanks for your link.

  5. #5
    Epy
    Epy is offline
    Hen-teaser Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    880
    Quote Originally Posted by Mr.Generic View Post
    thanks both for your answer.
    I didn't google because I didn't know what exactly to google for :P,but anyway I get why it doesn't work now,also thanks for your link.
    Well wait, are you actually trying to ADD to the lower byte or replace it? The code from the original post pointed towards replacing it, I thought that's what you wanted to do.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    3
    I wanted to add lower byte I forgot about also carry operation that qwer states.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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