我如何获得整数二进制表示中奇数位置上的所有位?

How do i get all the bits present on odd positions in binary representation of integers?

这些位从左到右枚举,从最左边的设置位开始,即最高有效位。例如,77的二进制表示为1001101。二进制表示中第1、3、5、7位的位分别为1,0,1,1。

Python:

int_val = 77
binary_val = bin(int_val)[2:]
odd_pos_data = [ value for index,  value in enumerate(binary_val,1) if index % 2 != 0 ]
print(odd_pos_data)

结果:

['1', '0', '1', '1']

This is how to do in C++:

#include <iostream>
#include <bitset>

using namespace std;

int main()
{   
    int num ;
    cout<<"Enter value : \n";
    cin >> num;
    std::string binary = std::bitset<8>(num).to_string(); //to binary
    cout<<"Binary value : \n\n";
    std::cout<<binary<<"\n\n";
    std::cout<<"Odd Index values:\n";
     for (int i = 0; i < binary.length(); i++) {
        if(i % 2 != 0){
        cout<< binary[i]<< " ";
        }
    }
    return 0;
}

结果: