Consider that the sign (+1 or -1) is known and there is a code that parses unsigned integer. That unsigned integer can be equal to -numeric_limits<int64_t>::max(). How to correctly compare without triggering undefined behavior?
int8_t sign = /* +1 or -1 */;
uint64_t result = /* parse the remaining string as unsigned integer */;
if( result > uint64_t(numeric_limits<int64_t>::max()))
{
if(sign == 1) return false; // error: out of range for int64_t
// Is the below code correct or how to implement correctly its intent?
if(result == uint64_t(-numeric_limits<int64_t>::min()))
{
return true;
}
return false;
}
As noted by Holt, you're effectively assuming 2's complement arithmetic. Therefore, you can replace -min by max+1:
if(result == uint64_t(numeric_limits<int64_t>::max()) + 1)
This avoids the undefined behavior (signed integer overflow) that results when negating the minimal value.
It might be a good idea to verify your system really uses 2's complement (depends on how strictly you want to comply with the C++ standard). This can be achieved by comparing -max with min:
if (numeric_limits<int64_t>::max() + numeric_limits<int64_t>::min() == 0)
{
// If not two's complement:
// Too large absolute value == error, regardless of sign
return false;
// on all sane (2's complement) systems this will be optimized out
}
There are no possibilities for other relations between min and max; this is explained here.
Related
So basically i need to check if a certain sequence of bits occurs in other sequence of bits(32bits).
The function shoud take 3 arguments:
n right most bits of a value.
a value
the sequence where the n bits should be checked for occurance
The function has to return the number of bit where the desired sequence started. Example chek if last 3 bits of 0x5 occur in 0xe1f4.
void bitcheck(unsigned int source, int operand,int n)
{
int i,lastbits,mask;
mask=(1<<n)-1;
lastbits=operand&mask;
for(i=0; i<32; i++)
{
if((source&(lastbits<<i))==(lastbits<<i))
printf("It start at bit number %i\n",i+n);
}
}
Your loop goes too far, I'm afraid. It could, for example 'find' the bit pattern '0001' in a value ~0, which consists of ones only.
This will do better (I hope):
void checkbit(unsigned value, unsigned pattern, unsigned n)
{
unsigned size = 8 * sizeof value;
if( 0 < n && n <= size)
{
unsigned mask = ~0U >> (size - n);
pattern &= mask;
for(int i = 0; i <= size - n; i ++, value >>= 1)
if((value & mask) == pattern)
printf("pattern found at bit position %u\n", i+n);
}
}
I take you to mean that you want to take source as a bit array, and to search it for a bit sequence specified by the n lowest-order bits of operand. It seems you would want to perform a standard mask & compare; the only (minor) complication being that you need to scan. You seem already to have that idea.
I'd write it like this:
void bitcheck(uint32_t source, uint32_t operand, unsigned int n) {
uint32_t mask = ~((~0) << n);
uint32_t needle = operand & mask;
int i;
for(i = 0; i <= (32 - n); i += 1) {
if (((source >> i) & mask) == needle) {
/* found it */
break;
}
}
}
There are some differences in the details between mine and yours, but the main functional difference is the loop bound: you must be careful to ignore cases where some of the bits you compare against the target were introduced by a shift operation, as opposed to originating in source, lest you get false positives. The way I've written the comparison makes it clearer (to me) what the bound should be.
I also use the explicit-width integer data types from stdint.h for all values where the code depends on a specific width. This is an excellent habit to acquire if you want to write code that ports cleanly.
Perhaps:
if((source&(maskbits<<i))==(lastbits<<i))
Because:
finding 10 in 11 will be true for your old code. In fact, your original condition will always return true when 'source' is made of all ones.
I'm trying to check a particular bit of a long long integer
long long int val=23355665641326;
int bit_no=32;
if( (val & (1<<bit_no)) == 0)
cout<<bit_no<<"'th bit is not set\n";
else
cout<<bit_no<<"'th bit is set\n";
the binary equivalent of 23355665641326 is -
101010011110111101010001001110110111101101110
^
we see, 32'th bit is set. But my code returns not set :(
how can i check the bit?
Your life would be easy if you use std::bitset instead:
constexpr auto N = CHAR_BIT * sizeof(long long int);
std::bitset<N> val(23355665641326LL);
Now you can test a particular bit as:
if ( val.test(i) ) {
//bit at index i is set!
}
Or you can use the faster — but unsafe — version, as:
if ( val[i] ) {
//bit at index i is set!
}
test() performs bound check and throws std::out_of_range if the index is invalid, whereas operator[] does not, in which case invalid index leads to undefined behavior.
You can use 1LL<<bit_no to check the bit status of a long long int.
As you are dealing with long long int you need to use long long type 1
Because if you use normal 1(int), you can check upto 31 bit
So just change the checking portion like this -
if( (val & (1LL<<bit_no) ) == 0)
^^^
To overcome with the need of adding the suffix to the check simply do something like this:
if(!!((val >> bit_no) & 1))
std::cout << "not set";
else
std::cout << "is set";
I'm new to C++. As part of an assignment we have to write to functions, but I don't know what the teacher mean by what he is requesting. Has anyone seen this or at least point me in the right direction. I don't want you to write the function, I just don't know what the output or what he is asking. I'm actually clueless right now.
Thank you
convertCtoD( )
This function is sent a null terminated character array
where each character represents a Decimal (base 10) digit.
The function returns an integer which is the base 10 representation of the characters.
convertBtoD( )
This function is sent a null terminated character array
where each character represents a Binary (base 2) digit.
The function returns an integer which is the base 10 representation of the character.
This function is sent a null terminated character array where each character represents a Decimal (base 10) digit. The function returns an integer which is the base 10 representation of the characters.
I'll briefly mention the fact that "an integer which is the base 10 representation of the characters" is useless here, the integer will represent the value whereas "base 10 representation" is the presentation of said value.
However, the desription given simply means you take in a (C-style) string of digits and put out an integer. So you would start with:
int convertCtoD(char *decimalString) {
int retVal = 0
// TBD: needs actual implementation.
return retVal;
}
This function is sent a null terminated character array where each character represents a Binary (base 2) digit. The function returns an integer which is the base 10 representation of the character.
This will be very similar:
int convertBtoD(char *binaryString) {
int retVal = 0
// TBD: needs actual implementation.
return retVal;
}
You'll notice I've left the return type as signed even though there's no need to handle signed values at all. You'll see why in the example implementation I provide below as I'm using it to return an error condition. The reason I'm providing code even though you didn't ask for it is that I think five-odd years is enough of a gap to ensure you can't cheat by passing off my code as your own :-)
Perhaps the simplest example would be:
int convertCToD(char *decimalStr) {
// Initialise accumulator to zero.
int retVal = 0;
// Process each character.
while (*str != '\0') {
// Check character for validity, add to accumulator (after
// converting char to int) then go to next character.
if ((*str < '0') || (*str > '9')) return -1;
retVal *= 10;
retVal += *str++ - '0';
}
return retVal;
}
The binary version would basically be identical except that it would use '1' as the upper limit and 2 as the multiplier (as opposed to '9' and 10).
That's the simplest form but there's plenty of scope for improvement to make your code more robust and readable:
Since the two functions are very similar, you could refactor out the common bits so as to reduce duplication.
You may want to consider an empty string as invalid rather than just returning zero as it currently does.
You probably want to detect overflow as an error.
With those in mind, it may be that the following is a more robust solution:
#include <stdbool.h>
#include <limits.h>
int convertBorCtoD(char *str, bool isBinary) {
// Configure stuff that depends on binary/decimal choice.
int maxDigit = isBinary ? '1' : '9';
int multiplier = maxDigit - minDigit + 1;
// Initialise accumulator to zero.
int retVal = 0;
// Optional check for empty string as error.
if (*str == '\0') return -1;
// Process each character.
while (*str != '\0') {
// Check character for validity.
if ((*str < '0') || (*str > maxDigit)) return -1;
// Add to accumulator, checking for overflow.
if (INT_MAX / multiplier < retVal) return -1;
retVal *= multiplier;
if (INT_MAX - (*str - '0') < retVal) return -1;
retVal += *str++ - '0';
}
return retVal;
}
int convertCtoD(char *str) { return convertBorCtoD(str, false); }
int convertBtoD(char *str) { return convertBorCtoD(str, true); }
As I read, in signed arithmetic there are many cases of undefined behaviour. Thus, I prefer to calculate results (even signed ones) using unsigned arithmetic, which is specified without undefined cases.
However, when the result is obtained using unsigned arithmetic, the last step, the conversion to the signed value remains to be done.
Here is a code I wrote and my question is if the code works in accordance with the rules, i.e., whether it is safe, not relying on some undefined/unspecified behaviour?
/*
function to safely convert given unsigned value
to signed result having the required sign
sign == 1 means the result shall be negative,
sign == 0 means the result shall be nonnegative
returns 1 on success, 0 on failure
*/
int safe_convert(unsigned value, int sign, int *result) {
if (sign) {
if (value > -(unsigned)INT_MIN) return 0; // value too big
if (!value) return 0; // cannot convert zero to negative int
*result = INT_MIN + (int)((-(unsigned)INT_MIN) - value);
} else {
if (value > (unsigned)INT_MAX) return 0; //value too big
*result = (int)value;
}
return 1;
}
Eventually, is there a way that is simpler, not relying on undefined/unspecified behaviour and doing the same thing?
Eventually, is there a way that is simpler, not relying on undefined behaviour and doing the same thing?
short x = (short) value;
int y = (int) value;
But be sure on what integral type you are casting to. value may go out of the range of the signed type used.
The only value that could be problematic is INT_MIN. Therefore I would just do something like
int safe_convert(unsigned value, int sign, int *result) {
if (sign) {
if (value > -(unsigned)INT_MIN) return 0; // value too big
if (-(unsigned)INT_MIN > (unsigned)INT_MAX // compile constant
&&
value == -(unsigned)INT_MIN) // special case
*result = INT_MIN;
else *result = -(int)value;
} else {
if (value > (unsigned)INT_MAX) return 0; //value too big
*result = (int)value;
}
return 1;
}
I don't think that the case of asking for a negative zero justifies an error return.
Conversion from unsigned to signed is not undefined, but implementation defined. From C++ Standard, chapter 4.7 Integral conversions, paragraph 3:
If the destination type is signed, the value is unchanged if it can be represented in the destination type (and
bit-field width); otherwise, the value is implementation-defined
Therefore the following is implementation defined and on many platforms exactly what you may expect (wrap around):
unsigned u = -1;
int i = (int)u;
The condition when sign is false (a positive number) is all ready well handled, it is when sign is true (a negative number) is tricky. So rather than:
if (value > -(unsigned)INT_MIN) return 0; // value too big
*result = INT_MIN + (int)((-(unsigned)INT_MIN) - value);
suggest
// 1st half is for 2's compliment machines
// 2nd half is for symmetric ints like 1's compliment and signed ints
// Optimization will simplify the real code to 1 compare
if ((((INT_MIN + 1) == -INT_MAX) && (value > ((unsigned)INT_MAX + 1u))) ||
(( INT_MIN == -INT_MAX) && (value > (unsigned)INT_MAX ))) return 0;
int i = (int) value;
*result = -i;
The INT_MIN == -INT_MAX tests could be used to conditionally allow a signed zero.
I'm about to do this in C++ but I have had to do it in several languages, it's a fairly common and simple problem, and this is the last time. I've had enough of coding it as I do, I'm sure there must be a better method, so I'm posting here before I write out the same long winded method in yet another language;
Consider the (lilies!) following code;
// I want the difference between these two values as a positive integer
int x = 7
int y = 3
int diff;
// This means you have to find the largest number first
// before making the subtract, to keep the answer positive
if (x>y) {
diff = (x-y);
} else if (y>x) {
diff = (y-x);
} else if (x==y) {
diff = 0;
}
This may sound petty but that seems like a lot to me, just to get the difference between two numbers. Is this in fact a completely reasonable way of doing things and I'm being unnecessarily pedantic, or is my spidey sense tingling with good reason?
Just get the absolute value of the difference:
#include <cstdlib>
int diff = std::abs(x-y);
Using the std::abs() function is one clear way to do this, as others here have suggested.
But perhaps you are interested in succinctly writing this function without library calls.
In that case
diff = x > y ? x - y : y - x;
is a short way.
In your comments, you suggested that you are interested in speed. In that case, you may be interested in ways of performing this operation that do not require branching. This link describes some.
#include <cstdlib>
int main()
{
int x = 7;
int y = 3;
int diff = std::abs(x-y);
}
All the existing answers will overflow on extreme inputs, giving undefined behaviour. #craq pointed this out in a comment.
If you know that your values will fall within a narrow range, it may be fine to do as the other answers suggest, but to handle extreme inputs (i.e. to robustly handle any possible input values), you cannot simply subtract the values then apply the std::abs function. As craq rightly pointed out, the subtraction may overflow, causing undefined behaviour (consider INT_MIN - 1), and the std::abs call may also cause undefined behaviour (consider std::abs(INT_MIN)). It's no better to determine the min and max of the pair and to then perform the subtraction.
More generally, a signed int is unable to represent the maximum difference between two signed int values. The unsigned int type should be used for the output value.
I see 3 solutions. I've used the explicitly-sized integer types from stdint.h here, to close the door on uncertainties like whether long and int are the same size and range.
Solution 1. The low-level way.
// I'm unsure if it matters whether our target platform uses 2's complement,
// due to the way signed-to-unsigned conversions are defined in C and C++:
// > the value is converted by repeatedly adding or subtracting
// > one more than the maximum value that can be represented
// > in the new type until the value is in the range of the new type
uint32_t difference_int32(int32_t i, int32_t j) {
static_assert(
(-(int64_t)INT32_MIN) == (int64_t)INT32_MAX + 1,
"Unexpected numerical limits. This code assumes two's complement."
);
// Map the signed values across to the number-line of uint32_t.
// Preserves the greater-than relation, such that an input of INT32_MIN
// is mapped to 0, and an input of 0 is mapped to near the middle
// of the uint32_t number-line.
// Leverages the wrap-around behaviour of unsigned integer types.
// It would be more intuitive to set the offset to (uint32_t)(-1 * INT32_MIN)
// but that multiplication overflows the signed integer type,
// causing undefined behaviour. We get the right effect subtracting from zero.
const uint32_t offset = (uint32_t)0 - (uint32_t)(INT32_MIN);
const uint32_t i_u = (uint32_t)i + offset;
const uint32_t j_u = (uint32_t)j + offset;
const uint32_t ret = (i_u > j_u) ? (i_u - j_u) : (j_u - i_u);
return ret;
}
I tried a variation on this using bit-twiddling cleverness taken from https://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax but modern code-generators seem to generate worse code with this variation. (I've removed the static_assert and the comments.)
uint32_t difference_int32(int32_t i, int32_t j) {
const uint32_t offset = (uint32_t)0 - (uint32_t)(INT32_MIN);
const uint32_t i_u = (uint32_t)i + offset;
const uint32_t j_u = (uint32_t)j + offset;
// Surprisingly it helps code-gen in MSVC 2019 to manually factor-out
// the common subexpression. (Even with optimisation /O2)
const uint32_t t = (i_u ^ j_u) & -(i_u < j_u);
const uint32_t min = j_u ^ t; // min(i_u, j_u)
const uint32_t max = i_u ^ t; // max(i_u, j_u)
const uint32_t ret = max - min;
return ret;
}
Solution 2. The easy way. Avoid overflow by doing the work using a wider signed integer type. This approach can't be used if the input signed integer type is the largest signed integer type available.
uint32_t difference_int32(int32_t i, int32_t j) {
return (uint32_t)std::abs((int64_t)i - (int64_t)j);
}
Solution 3. The laborious way. Use flow-control to work through the different cases. Likely to be less efficient.
uint32_t difference_int32(int32_t i, int32_t j)
{ // This static assert should pass even on 1's complement.
// It's just about impossible that int32_t could ever be capable of representing
// *more* values than can uint32_t.
// Recall that in 2's complement it's the same number, but in 1's complement,
// uint32_t can represent one more value than can int32_t.
static_assert( // Must use int64_t to subtract negative number from INT32_MAX
((int64_t)INT32_MAX - (int64_t)INT32_MIN) <= (int64_t)UINT32_MAX,
"Unexpected numerical limits. Unable to represent greatest possible difference."
);
uint32_t ret;
if (i == j) {
ret = 0;
} else {
if (j > i) { // Swap them so that i > j
const int32_t i_orig = i;
i = j;
j = i_orig;
} // We may now safely assume i > j
uint32_t magnitude_of_greater; // The magnitude, i.e. abs()
bool greater_is_negative; // Zero is of course non-negative
uint32_t magnitude_of_lesser;
bool lesser_is_negative;
if (i >= 0) {
magnitude_of_greater = i;
greater_is_negative = false;
} else { // Here we know 'lesser' is also negative, but we'll keep it simple
// magnitude_of_greater = -i; // DANGEROUS, overflows if i == INT32_MIN.
magnitude_of_greater = (uint32_t)0 - (uint32_t)i;
greater_is_negative = true;
}
if (j >= 0) {
magnitude_of_lesser = j;
lesser_is_negative = false;
} else {
// magnitude_of_lesser = -j; // DANGEROUS, overflows if i == INT32_MIN.
magnitude_of_lesser = (uint32_t)0 - (uint32_t)j;
lesser_is_negative = true;
}
// Finally compute the difference between lesser and greater
if (!greater_is_negative && !lesser_is_negative) {
ret = magnitude_of_greater - magnitude_of_lesser;
} else if (greater_is_negative && lesser_is_negative) {
ret = magnitude_of_lesser - magnitude_of_greater;
} else { // One negative, one non-negative. Difference is sum of the magnitudes.
// This will never overflow.
ret = magnitude_of_lesser + magnitude_of_greater;
}
}
return ret;
}
Well it depends on what you mean by shortest. The fastet runtime, the fastest compilation, the least amount of lines, the least amount of memory. I'll assume you mean runtime.
#include <algorithm> // std::max/min
int diff = std::max(x,y)-std::min(x,y);
This does two comparisons and one operation (this one is unavoidable but could be optimized through certain bitwise operations with specific cases, compiler might actually do this for you though). Also if the compiler is smart enough it could do only one comparison and save the result for the other comparison. E.g if X>Y then you know from the first comparison that Y < X but I'm not sure if compilers take advantage of this.