简体   繁体   中英

Bit masking to determine if number is positive or negative c++

I want to replicate the behaviour of a micro controller.

If the memory location of the program counter contains 0x26 then I want to check that the value in the next memory location is positive or negative.

If it is positive then I add it to the program counter PC and if it is negative then I add it to the program counter PC , which is essentially subtracting it.

I am using bit masking to do this but I am having issues determining a negative value.

           {
                if (value_in_mem && 128 == 128)
                {
                    cout << "\nNext byte is : " << value_in_mem << endl;
                    cout << "\nNumber is positive!" << endl;
                    PC = PC + value_in_mem;
                    cout << "\n(Program Counter has been increased)" << endl;

                }
                else if (value_in_mem && 128 == 0)
                {
                    cout << "\nNext byte is : - " << value_in_mem << endl;
                    cout << "\nNumber is negative!" << endl;
                    PC = PC + value_in_mem;
                    cout << "\n(Program Counter has been decreased)" << endl;
                }
            }

My method is to && the value_in_mem (an 8 bit signed int) with 128 ( 0b10000000 ) to determine if the most significant bit is 1 or 0, negative or postitve respectively.

value_in_mem is a 8-bit hexadecimal value and I think this is where my confusion lies. I'm not entirely sure how negative hexadecimal values work, could someone possibly explain this and the errors in my attempt at the code?

1) You're using && which is a logical AND but you should use & which is a bitwise AND .

// It would be better to use hex values when you're working with bits
if ( value_in_mem & 0x80 == 0x80 )
{
    // it's negative
}
else
{
    // it's positive
}

2) You can simply compare your value to 0 (if value_in_mem is declared as char )

if ( value_in_mem < 0 )
{
    // it's negative
}
else
{
    // it's positive
}

Make sure you are using correct types for your values (or cast them where it matters, if you prefer for example to have memory values as unsigned bytes most of the time (I certainly would), then cast it to signed 8 bit integer only for the particular calculation/comparison by static_cast<int8_t>(value_in_mem) ).

To demonstrate the importance of correct typing, and how C++ compiler will then do all the dirty work for you, so you don't have to bother with bits and can use also if (x < 0) :

#include <iostream>

int main()
{
    {
        uint16_t pc = 65530;     int8_t b = 0xFF;     pc += b;
        std::cout << pc << "\n"; // unsigned 16 + signed 8
        // 65529 (b works as -1, 65530 - 1 = 65529)
    }
    {
        int16_t pc = 65530;      int8_t b = 0xFF;     pc += b;
        std::cout << pc << "\n"; // signed 16 + signed 8
        // -7 (b works as -1, 65530 as int16_t is -6, -6 + -1 = -7)
    }
    {
        int16_t pc = 65530;      uint8_t b = 0xFF;    pc += b;
        std::cout << pc << "\n"; // signed 16 + unsigned 8
        // 249 (b works as +255, 65530 as int16_t is -6, -6 + 255 = 249)
    }
    {
        uint16_t pc = 65530;     uint8_t b = 0xFF;    pc += b;
        std::cout << pc << "\n"; // unsigned 16 + unsigned 8
        // 249 (b = +255, 65530 + 255 = 65785 (0x100F9), truncated to 16 bits = 249)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM