c++ simple? problem

Discussion in 'Programmer's Corner' started by hearry, Mar 23, 2009.

  1. hearry

    Thread Starter New Member

    Jan 18, 2009
    7
    0
    hello, im trying to capture one bit form int var, and then after its gone trough some operators to write that bit on the same place.

    so for capturin i wrote: (lets say i need first bit captured and that int var is a with lenght of 2bytes)

    g=0x8000 & a;

    as far i can see that bit will be written in g var (int) and all others will be 0. Now i cant figure out how to put that same bit i got in a var later.

    no need saying im beginner..
     
  2. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    All you should need to do is get the state of the bit of interest then clear the bit in the original int variable. You can then "or" the resulting bit state obtained following the with the operation that you performed on the bit of interest.

    hgmjr
     
  3. hearry

    Thread Starter New Member

    Jan 18, 2009
    7
    0
    i see what you mean, but the more i think about it the less i know

    so far there is

    int a=-10;
    int g=0x80000000;

    now lets say I add number 20 to a var. out would be 10, now im trying to put that first bit in a var that g var is holding so -sign shows up.

    the thing you said about clearing first bit from a var and then or with g. I see it would work |ing will change first bit if needed and keep rest of the a value unchanged. Now supid try was to shift bits in a var so 0 comes up :D. I got sign "-" right but value not so much.

    this faliure looked like this: cout <<(((a+20)>>1)|g);

    looks like i need to sit and warm up a chair, thanks for the help.
     
  4. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Did you mean to the variable "g" into a 32-bit integer in your example? I thought we were just dealing with 16-bit integer values.

    hgmjr
     
  5. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    I tried to understand your explanation but could not decide what would be the final result you were looking for in your answer.

    Perhaps you could tell me what the correct value you expect after you performed all of your operations. Then maybe things would clear up for me.

    hgmjr
     
  6. hearry

    Thread Starter New Member

    Jan 18, 2009
    7
    0
    my mistake in first post i wrote g=0x8000 but in fact it should be 0x80000000 a 4byte int.

    what im trying to do is: 1. input some random integer A; 2. read and store sign(+or-) bit into variable G; 3. do left shift multiplication(or something that will change sign) on A var. 4. return the sign stored in G to A.

    in my failed example:

    int a=-10;
    int g=(0x80000000&a);
    cout <<(((a+20)>>1)|g);

    a+20 is +10 then tried to clear first bit by shifting:D then tried to put the sign - as it were in initial value of A. so i was looking for output like -10. if it were a+30 result is positive 20 and with sign - stored in G result would be -20.
    operation on A is not so important like geting original sign(in this case - ) back to result.
     
  7. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Still a bit confusing. Sorry.

    Can you just write the resulting value that you expect "cout" to end up being.

    hgmjr
     
  8. hearry

    Thread Starter New Member

    Jan 18, 2009
    7
    0
    ok

    int a=-10;
    int g=0x80000000 & a;

    int sumd=a+30;

    cout should be -20 same value, (-) sign form variable a.
     
  9. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    If you set g = sumd wouldn't that accomplish your goal?

    hgmjr
     
  10. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Silly me, that would end up with cout being a positive 20 when you want it to negative didn't you.

    you could try

    sumd = (a + 30) | g;

    hgmjr
     
Loading...