I now know how it's done in one line, altough I fail to realise why my first draft doesn't work aswell. What I'm trying to do is saving the lower part into a different variable, shifting the higher byte to the right and adding the two numbers via OR. However, it just cuts the lower half of the hexadecimal and returns the rest.
short int method(short int number) {
short int a = 0;
for (int x = 8; x < 16; x++){
if ((number & (1 << x)) == 1){
a = a | (1<<x);
}
}
number = number >> 8;
short int solution = number | a;
return solution;
You are doing it one bit at a time; a better approach would do it with a single operation:
uint16_t method(uint16_t number) {
return (number << 8) | (number >> 8);
}
The code above specifies 16-bit unsigned type explicitly, thus avoiding issues related to sign extension. You need to include <stdint.h> (or <cstdint> in C++) in order for this to compile.
if ((number & (1 << x)) == 1)
This is only going to return true if x is 0. Since 1 in binary is 00000000 00000001, and 1 << x is going to set all but the x'th bit to 0.
You don't care if it's 1 or not, you just care if it's non-zero. Use
if (number & (1 << x))
Related
Question is Given - To Count Total number of set bits for 2 given numbers . For example take 2 and 3 as input . So 2 represents - 10 in binary form and 3 represents - 11 in binary form , So total number of set bits = 3.
My work -
#include <iostream>
using namespace std;
int bit(int n1, int n2){
int count = 0;
while(n1 != 0 && n2 != 0){
if(n1 & 1 || n2 & 1) {
count++;
}
n1 >> 1;
n2 >> 1;
}
return count;
}
int main() {
int a;
cin >> a;
int b;
cin >> b;
cout << bit(a,b);
return 0;
}
Expected Output - 3
So please anyone knows what i am doing wrong please correct it and answer it.
Why ask the question for 2 numbers if the intended combined result is just the sum of the separate results?
If you can use C++20, std::popcount gives you the number of set bits in one unsigned variable.
If you can't use C++20, there is std::bitset. Construct one from your number and use its method count.
So your code becomes:
int bit(int n1, int n2) {
#if defined(__cpp_lib_bitops)
return std::popcount(static_cast<unsigned int>(n1)) + std::popcount(static_cast<unsigned int>(n2));
#else
return std::bitset<sizeof(n1)*8>(n1).count() + std::bitset<sizeof(n2)*8>(n2).count();
#endif
}
Demo
I'm not quite sure what happens if you give negative integers as input. I would do a check beforehand or just work with unsigned types from the beginning.
What you are doing wrong was shown in a (now deleted) answer by Ankit Kumar:
if(n1&1 || n2&1){
count++;
}
If a bit is set in both n1 and n2, you are including it only once, not twice.
What should you do, other than using C++20's std::popcount, is to write an algorithm1 that calculates the number of bits set in a number and then call it twice.
Also note that you should use an unsigned type, to avoid implementation defined behavior of right shifting a (possibly) signed value.
1) I suppose that is the purpose of OP's exercise, everybody else shuold just use std::popcount.
It is possible without a loop, no if and only 1 variable.
First OR the values together
int value = n1 | n2; // get the combinedd bits
Then divide value in bitblocks
value = (value & 0x55555555) + ((value >> 1) & 0x55555555);
value = (value & 0x33333333) + ((value >> 1) & 0x33333333);
value = (value & 0x0f0f0f0f) + ((value >> 1) & 0x0f0f0f0f);
value = (value & 0x00ff00ff) + ((value >> 1) & 0x00ff00ff);
value = (value & 0x0000ffff) + ((value >> 1) & 0x0000ffff);
This works for 32bit values only, adjust fopr 64bit accordingly
how can I turn off leftmost non-zero bit of a number in O(1)?
for example
n = 366 (base 10) = 101101110 (in base 2)
then after turning the leftmost non-zero bit off ,number looks like = 001101110
n will always be >0
Well, if you insist on O(1) under any circumstances, the Intel Intrinsics function _bit_scan_reverse() defined in immintrin.h does a hardware find for the most-significant non-zero bit in a int number.
Though the operation does use a loop (functional equivalent), I believe its constant time given its latency at fixed 3 (as per Intel Intrinsics Guide).
The function will return the index to the most-significant non-zero bit thus doing a simple:
n = n & ~(1 << _bit_scan_reverse(n));
should do.
This intrinsic is undefined for n == 0. So you gotta watch out there. I'm following the assumption of your original post where n > 0.
n = 2^x + y.
x = log(n) base 2
Your highest set bit is x.
So in order to reset that bit,
number &= ~(1 << x);
Another approach:
int highestOneBit(int i) {
i |= (i >> 1);
i |= (i >> 2);
i |= (i >> 4);
i |= (i >> 8);
i |= (i >> 16);
return i - (i >> 1);
}
int main() {
int n = 32767;
int z = highestOneBit(n); // returns the highest set bit number i.e 2^x.
cout<< (n&(~z)); // Resets the highest set bit.
return 0;
}
Check out this question, for a possibly faster solution, using a processor instruction.
However, an O(lgN) solution is:
int cmsb(int x)
{
unsigned int count = 0;
while (x >>= 1) {
++count;
}
return x & ~(1 << count);
}
If ANDN is not supported and LZCNT is supported, the fastest O(1) way to do it is not something along the lines of n = n & ~(1 << _bit_scan_reverse(n)); but rather...
int reset_highest_set_bit(int x)
{
const int mask = 0x7FFFFFFF; // 011111111[...]
return x & (mask >> __builtin_clz(x));
}
how to count number of occurrences of 1 in a 8 bit string. such as 10110001.
bit string is taken from user. like 10110001
what type of array should be used to store this bit string in c?
Short and simple. Use std::bitset(C++)
#include <iostream>
#include <bitset>
int main()
{
std::bitset<8> mybitstring;
std::cin >> mybitstring;
std::cout << mybitstring.count(); // returns the number of set bits
}
Online Test at Ideone
Don't use an array at all, use a std::string. This gives you the possibility of better error handling. You can write code like:
bitset <8> b;
if ( cin >> b ) {
cout << b << endl;
}
else {
cout << "error" << endl;
}
but there is no way of finding out which character caused the error.
You'd probably use an unsigned int to store those bits in C.
If you're using GCC then you can use __builtin_popcount to count the one bits:
Built-in Function: int __builtin_popcount (unsigned int x)
Returns the number of 1-bits in x.
This should resolve to a single instruction on CPUs that support it too.
From hacker's delight:
For machines that don't have this instruction, a good way to count the number
of 1-bits is to first set each 2-bit field equal to the sum of the two single
bits that were originally in the field, and then sum adjacent 2-bit fields,
putting the results in each 4-bit field, and so on.
so, if x is an integer:
x = (x & 0x55555555) + ((x >> 1) & 0x55555555);
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
x = (x & 0x0F0F0F0F) + ((x >> 4) & 0x0F0F0F0F);
x = (x & 0x00FF00FF) + ((x >> 8) & 0x00FF00FF);
x = (x & 0x0000FFFF) + ((x >>16) & 0x0000FFFF);
x will now contain the number of 1 bits. Just adapt the algorithm with 8 bit values.
I'm trying to optimize some bit packing and unpacking routines. In order to do the packing I need to calculate the number of bits needed to store integer values. Here is the current code.
if (n == -1) return 32;
if (n == 0) return 1;
int r = 0;
while (n)
{
++r;
n >>= 1;
}
return r;
Non-portably, use the bit-scan-reverse opcode available on most modern architectures. It's exposed as an intrinsic in Visual C++.
Portably, the code in the question doesn't need the edge-case handling. Why do you require one bit for storing 0? In any case, I'll ignore the edges of the problem. The guts can be done efficiently thus:
if (n >> 16) { r += 16; n >>= 16; }
if (n >> 8) { r += 8; n >>= 8; }
if (n >> 4) { r += 4; n >>= 4; }
if (n >> 2) { r += 2; n >>= 2; }
if (n - 1) ++r;
You're looking to determine the integer log base 2 of a number (the l=highest bit set). Sean Anderson's "Bit Twiddling Hacks" page has several methods ranging from the obvious counting bits in a loop to versions that use table lookup. Note that most of the methods demonstrated will need to be modified a bit to work with 64-bit ints if that kind of portability is important to you.
http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious
Just make sure that any shifting you're using to work out the highest bit set needs to be done' on an unsigned version of the number since a compiler implementation might or might not sign extend the >> operation on a signed value.
What you are trying to do is find the most significant bit. Some architectures have a special instruction just for this purpose. For those that don't, use a table lookup method.
Create a table of 256 entries, wherein each element identifies the upper most bit.
Either loop through each byte in the number, or use a few if-statements to break to find the highest order non-zero byte.
I'll let you take the rest from here.
Do a binary search instead of a linear search.
if ((n >> 16) != 0)
{
r += 16;
n >>= 16;
}
if ((n >> 8) != 0)
{
r += 8;
n >>= 8;
}
if ((n >> 4) != 0)
{
r += 4;
n >>= 4;
}
// etc.
If your hardware has bit-scan-reverse, an even faster approach would be to write your routine in assembly language. To keep your code portable, you could do
#ifdef ARCHITECTURE_WITH_BSR
asm // ...
#else
// Use the approach shown above
#endif
You would have to check the execution time to figure the granularity, but my guess is that doing 4 bits at a time, and then reverting to one bit at a time would make it faster. Log operations would probably be slower than logical/bit operations.
if (n < 0) return 32;
int r = 0;
while (n && 0x7FFFFFF0) {
r+=4;
n >>= 4; }
while (n) {
r++;
n >>= 1; }
return r;
number_of_bits = log2(integer_number)
rounded to the higher integer.
Is this even at all possible?
How would I truncate zeros?
In the integer withOUT using any masking techniques (NOT ALLOWED: 0x15000000 & 0xff000000 like that.). And also WITHOUT any casting.
Well, really, if you want to truncate the right side, the naive solution is:
uint input = 0x150000;
if(input)
{
while(!(input & 0x01)) // Replace with while(!(input % 0x10)) if you are actually against masking.
{
input >>= 1;
}
}
// input, here, will be 0x15.
Though, you can unroll this loop. As in:
if(!(input & 0xFFFF)) { input >>= 16; }
if(!(input & 0x00FF)) { input >>= 8; }
if(!(input & 0x000F)) { input >>= 4; } // Comment this line, down, if you want to align on bytes.
if(!(input & 0x0003)) { input >>= 2; } // Likewise here, down, to align on nybbles.
if(!(input & 0x0001)) { input >>= 1; }
One way to do it without any masking (assuming you want to truncate zero bits):
int input = 0x150000;
while (input && !(input%2))
input >>= 1;
Here's a complete program which illustrates it.
#include <stdio.h>
int main (int argc, char *argv[]) {
int input = 0;
if (argc < 2) {
fprintf (stderr, "Needs at least one parameter.\n");
return 1;
}
input = atoi (argv[1]);
printf ("%x -> ", input);
while (input && !(input%2))
input >>= 1;
printf ("%x\n",input);
return 0;
}
If you want to truncate zero nybbles, use:
while (input && ((input%16)==0))
input >>= 4;
John Gietzen's answer is my favourite, but for fun, it can actually be done without a loop!
If you know how many trailing zeros there are, then you can just shift right that number of bits. There are a number of techniques for finding the number of bits. See the few sections following the linear algorithm here: http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightLinear.
Divide by 16 (one nybble or hex digit's worth), as long as it's a multiple of 16:
if ( number )
while ( number % 16 == 0 )
number /= 16;
Of course, you can drop the initial test for zero if you know you'll never that as input.
Does this count as masking?
unsigned int truncate(unsigned int input)
{
while (input != 0 && input % 0x10 == 0)
{
input /= 0x10;
}
return input;
}
The easiest way is to convert the int to a byte, so you are now down to 8 bits, so not everything is truncated, necessarily, but it would have fewer zeroes.
I am hoping this isn't for homework, but because I fear it is, giving you all the code would not be fair.
Shift right with 24. Make sure your variable is unsigned.