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.
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 12 months ago.
Improve this question
I want to fill a 2D array that is initialized with 0's to a binary array that indicates all possible combinations of the columns. As input variable, I only have the number of alternatives. From this input, I calculate the possible combinations of the alternatives. Next, I create a 2D array with the number of rows equal to the number of possible combinations and with the number of columns equal to the number of alternatives that was given as input. I initialize all values of the 2D arrays with a 0. Now I try to transform this 2D array to an array with 0's and 1's, indicating all possible combinations of alternatives. Below I have attached a practical example that clarifies the goal of the code.
Can anyone help my with writing the for loops that can solve my problem?
Thanks in advance!
Practical example
I think 0000 is one of the combinations, too.
You can find the patterns:
Each column repeats k 0s then k 1s where k = 2^(j+1).
E.g. 4 bits -> 2^4 combinations
int a[16][4] = {0};
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 4; j++) {
int k = (int)pow(2, j+1);
if (i % k + 1 > k / 2) {
a[i][j] = 1;
}
}
}
Output:
0000
1000
0100
1100
0010
1010
0110
1110
0001
1001
0101
1101
0011
1011
0111
1111
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 this data file in hexadecimal and I want to convert them to big endian from little endian but I come across this trouble in C++. C++ treats 2 hexa value as one to make a byte for char and always outputs decimal equivalent to those 2 combined hexa value but I don't want this.
Say I am reading a date, 8 byte in little endian:
e218 70d2 0e00 0000
Code:
char buffer;
for(int i = 0; i<BYTE_SIZE; ++i){
infile>>buffer;
cout<<"Read In Int = "<<(unsigned int)buffer<<endl;
cout<<"Read as Hex= "<<hex<<(int)buffer<<endl;
}
Output:
Read as Int= 226
Read as Hex= e2
-------------End of First Iter-------------------------
Read as Int= 18
Read as Hex= 18
...
So if you note that e2, which is in base 16, is concatenated as one entity. When you convert to int, you get: 14*16 + 2 = 226. I want to read e only and then 2 only and so on....
My main goal is to flip it and read it as 18e2 and convert that to decimal. How can I do this?
Update: I think people are misunderstanding this question. I simply wanted a way to read binary file containing hexa values and shift things around individually. This problem was hard to describe on text but anyway, Thank you to those who commented with possible solutions.
Played with bits and solved it using:
Preprocessing:
vector<bitset<64>> input;
for(int i = 0; i<SIZE; ++i){
infile>>buffer;
input.push_back(bitset<64>(buffer));
}
Here is the function:
unsigned long convert(vector<bitset<64>> input)
{
bitset<64> data ;
for(int i = input.size()-1; i>-1; --i)
data=data|(input[i]<<i*8);
return data.to_ullong();
}
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.
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 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 7 years ago.
Improve this question
I have these bits 0010 1110 0101 0111.
Currently the value of bits from 7 to 11 (right to left) is 10011. I want to set it to 10110 for example. How do I do that?
This topic is common in the world of embedded systems. Usually, manufacturers of hardware devices use bit fields to represent information, such as statuses.
Inserting Into Your Number
This involves left shifting your number (such as birth year) into the appropriate position then ORing the value with your number:
unsigned int value;
//...
value |= (birth_year << 1);
Extracting or Getting the Number:
You will need to AND the number with a mask so that only the important bits are extracted. For example, retrieving gender:
unsigned int gender;
unsigned int value;
gender = value & 1;
// or
gender = value & (~0);
You may need to right shift the bits to get the correct value, such as after extracting the birth year, right shift it by 1.
Bit Field Structure
You can let the Compiler figure all this by using bit fields in a structure, something like:
struct Compressed_Number
{
unsigned int gender : 1;
unsigned int birth_year : 11;
//..
};
I personally prefer the Boolean Arithmetic version because you always know the bit positions.