Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am interested in understanding the implementation of converting decimal to binary. Could somebody clarify the purpose of using left-shift and Right-shift in the following code?
void static inline unsignedToBinary(unsigned x, char*& bin)
{
bin = (char*) malloc(33);
int p = 0;
for (unsigned i = (1 << 31); i > 0; i >>= 1)
bin[p++] = ((x&i) == i) ? '1' : '0';
bin[p] = '\0';
}
This is a straightforward implementation of binary conversion that uses bit operations.
Variable i represents the mask - an int containing 2k value, where k is the position of the bit.
The initial value is 231, produced with left-shifting 1 by 31.
for loop uses >>= 1 to right-shift the mask until 1 gets shifted out of it, making i == 0.
At each iteration x&i is compared to i. The comparison succeeds when x contains 1 in the position where i has its 1; it fails otherwise.
Note: Although using malloc in C++ is certainly allowed, it is not ideal. If you want to stay with C strings, use new char[33] instead. A more C++-like approach would be using std::string.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
It keeps saying
Cannot implicitly convert type 'long' to 'int'. An explicit conversion exists (are you missing a cast?)
int lVar10 = 0x7a69;
int uVar12 = lVar10 * 0x1a3 + 0x181d;
lVar10 = uVar12 + ((uVar12 & 0xffffffff) / 0x7262) * -0x7262;
In short, the numbers you are trying to operate are bigger than the highest possible int value, specifically at the row
lVar10 = uVar12 + ((uVar12 & 0xffffffff) / 0x7262) * -0x7262;
In decimal that number is 2147483647 = 2^31 - 1. Since you are using hexa to represent numbers (with the prefix 0x), the 0xffffffff alone is already bigger than the highest integer.
So you could fix this by declaring lVar10 and lVar12 as long.
You should cast the variables to a long. Adding
(long) after the = in the third line should solve it.
Edit:
Also, you could just declare the variable lVar10 and uVar12 as long in the first place.
You are trying to store a non-integer into an integer variable.
The variable named lVar10 can be assigned integers. For example, values like -3, -2, -1, 0, 1, 2, 3.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a binary number such as 1011, I would like to split this number to the following numbers:
1000
0000
0010
0001
So that if I apply (or) " | " operator it will produce the original number :
1000 | 0000 | 0010 | 0001 = 1011.
Simply bitwise-and the number with a mask that has the position you want set to 1. For example
uint8_t input;
uint8_t least_significant_digit = input & 1;
You can produce these with a loop if necessary.
uint8_t input;
uint8_t output[8];
for (int i = 0; i < 8; ++i) {
int mask = 1 << (7 - i); // most significant digit first as in the example
output[i] = input & mask;
}
I wish I had a better name than output but I have no idea what this is called. You can play with the value of the loop condition depending on how many digits you actually need. Make sure to not make it larger than the type in question. For signed types you cannot (in theory) extract the sign bit with this method because bit-shifting into the sign bit is undefined behavior.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have the following piece of code,
if (index > ((v.size() >> 1) - 1)) { }
v.size() is 0 and index is 1. The execution does not get into the if block. But if I change the above code to,
int limit = (v.size() >> 1) - 1;
if (index > limit) { }
and with the same values of v.size() and index, the execution does get into the if block.
Why does this behavior happen? Thanks.
You are obviously performing unsigned comparison. The return type of v.size() is likely size_t, which is an unsigned integer. Thus, the expression (v.size() >> 1) - 1 yields the largest possible unsigned integer. Unless you convert that back to a signed integer type, you won't get positive comparison results.
A simple cast to ssize_t before the subtraction will fix your problem:
if (index > (((ssize_t)v.size() >> 1) - 1)) { }
I have used ssize_t because that type is guaranteed to be able to hold any size_t value, reinterpreting it to negative if its most significant bit is set. That cannot be said for int, as that is usually only 32 bits on 64 bit platforms. So, using int as you did in your second example, may cut off some bits.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Given an int variable, I would like to check if the number of '1' in its binary representation is even or odd. It can be made with xor operations like
int n;
int s = 0;
for(;n;n>>=1)
s ^= (n&1);
There's some better way to do so in C++?
Note: I'm not asking for the number of '1's, but for its parity, so I thought there could be some better code than mine.
uint32_t v = somevalue;
v ^= v >> 1;
v ^= v >> 2;
v = (v & 0x11111111U) * 0x11111111U;
bool parity = (v >> 28) & 1;
From https://graphics.stanford.edu/~seander/bithacks.html
It has a 64bit variant too.
For clarification, with "parity" I don't mean if the number is even or odd mathematically, but if the count of 1 bits in it's binary representation is even or odd; like described in https://en.wikipedia.org/wiki/Parity_bit. With the maths meaning, the code in the question makes no sense, so I assumed OP means the same. The statement
I'm not asking for the number of '1's, but for its parity
then means that he/she just wants to know if the 1 count is even or odd,
but not the exact number of 1's.
If you are really after speed, you can tabulate the number of bits (or just its parity) for all byte values 0..255. Then mapping a union on the variable or using shifts/masks, accumulate for the four bytes.
Even faster and more paranoid, tabulate for all short values 0..65535.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
How can I append an int to an int:
x = 23;
y = 54;
result = 2354;
I hope you help me.
Here are two general approaches:
"Shift" the left integer over by multiplying by the appropriate power of 10 and then add the right integer. In the example code that is x * 100 + y (or x * pow(10,2) + y) as shown in a comment.
The value to shift can be derived from the ceiling of the log10 of the right number. Using the math above, this could be expanded more generally as x * pow(10, ceil(log10(y))) + y.
Convert the integers to strings, concatenate the strings, and convert the resulting string back to an integer.
Take the inputs as a string
concatenate the strings. or you can use append to add the strings.