converting one byte into 8 bits with same data set - c++

I have a question. I have a data set of 32 by 32 integer matrix. As int holds 32 bit, i want to break down each elements of my matrix into 32 bit, suppose i have 255 decimal at first place of matrix and this can hold 32 bit as of integer, now i want to convert this 255 decimal into binary 255 i.e. 11111111 and padding with zero the remaining positions. I don't want to construct another matrix because then the data size will be greater than 32 bits. I want to do this with C++.
Something like this...
a[32][32];
for(int i=0;i<32;i++)
for(int j=0;j<32;j++)
a[i][j]=255+i+j;
Now let a[0][0]=255 in decimal form...i want to convert this and each element of matrix a into decimal and which will be like this..a[0][0]=00000000000000000000000011111111. This is 32 bit as int can hold 32 bit. Now my question is how i can access each bit of the a[0][0]. The important thing is that i want to stay in 32 bit format and don't want to create another matrix.
If you have any solution then please share with me. I am new to programming world. Thanks

To access bits of an integer you must use the bitwise operators.
To test if the nth bit is set use & and <<
if (m[0][0] & (1 << n))
cout << "bit " << n << " is set";
Remember to count from 0, so to test if the first (or lowest) bit is set use 1 << 0, the second use 1 << 1 etc.
To set the nth bit use |= and <<
m[0][0] |= (1 << n);
To clear the nth bit use &=, ~ and <<
m[0][0] &= ~(1 << n);
To invert the nth bit use ^= and <<
m[0][0] ^= (1 << n);
There are lots of other possibilities including testing or setting multiple bits at a time, extracting bit fields etc. I suggest you read a tutorial on this. There are lots and lots of these on the internet, for instance this one.

Related

How does the bit store in the array?

While doing my homework, I had a question about bits.
How does save bits in an array of int8_t?
And How can I access these bits?
Here are some example code
void someting_with_bits(int8_t bit_array[])
{
//Do sometings...
}
If there is a function like this,
when I call bit_array[0], do I return 8 length bits like 11100011?
If that be so, How can I access the first bits of bit_array[0]?
Yes, it will be saved like 11000011 in one array block.
To access a specified bit, you need to shift it and then AND it with a special mask.
For example, to access the highest bit of bit_array[0]:
int8_t highest_bit = (bit_array[0] >> 7) & 0x1;
to access the highest 4 bits of bit_array[0]:
int8_t highest_4_bits = (bit_array[0] >> 4) & 0xf;
If you want to access individual bits of each integer, you can do some bit manipulations.
for example if you want to check 3rd least significant bit of an integer, AND it with 0x4(100). Also you can right shift your int 2 times and then AND it with 0x1.
for example to check the 5th least significant bit of integer with index 3 in your array:
bool theBit = (bit_array[3] >> 4) & 0x1;
Of course you can use bitset.
http://www.cplusplus.com/reference/bitset/bitset/
for example to access 3rd least significant bit of a 32 bit integer
(don't forget to include bitset header):
int32_t number = 233;
std::bitset<32> bits(number);
std::cout << "The 3rd ls bit is: " << bits[2] << std::endl;

How do I display the bits in an unsigned int WITHOUT using bitset?

I am currently working on a project for school covering bit manipulation. We are supposed to show the bits for an unsigned integer variable and allow the user to manipulate them, turning them on and off and shifting them. I have all of the functionality working, except for displaying the bits once they have been manipulated. We are NOT allowed to use bitset to display the bits, and it will result in a heavy grade reduction.
I have tried using if statements to determine whether the bits are on or off, but this does not seem to be working. Whenever a bit is changed, it will simply print a lot of 0's and 1's.
std::cout << "Bits: ";
for (int i = sizeof(int)*8; i > 0; i--)
{
if (a | (0 << i) == 1)
std::cout << 1;
if (a | (0 << i) == 0)
std::cout << 0;
}
std::cout << std::endl << a;
I would expect that if I turn a bit on, that one bit will display a 1 instead of a 0, with the rest of the bits being unchanged and still displaying 0; instead it prints a string of 1010101 about the length of half the console.
There are a couple of problems here, and you might want to do a detailed review of bit manipulation:
for (int i = sizeof(int)*8; i > 0; i--) should be for (int i = sizeof(int)*8 - 1; i >= 0; i--), because bits are 0-indexed (shifting 1 to the left 0 times gives a set bit on the rightmost position).
We use bitwise AND (&) instead of bitwise OR (|) to check if a bit is set. This is because when we use bitwise AND with a number that only has a single bit set, the result will be a mask with the bit at the position of the 1 being in the same state as the corresponding bit in the original number (since anything AND 1 is itself), and all other bits being 0's (since anything AND 0 is 0).
We want a mask with 1 in the position that we want to check and 0 elsewhere, so we need 1 << i instead of 0 << i.
If the bit we're checking is set, we'll end up with a number that has one bit set, but that's not necessarily 1. So we should check if the result is not equal to 0 instead of checking if it's equal to 1.
The == operator has a higher precedence compared to the | and the & operators, so parenthesis is needed.

Get the low portion of a number of any of the built-in types [closed]

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 4 years ago.
Improve this question
How would I create a function template which returns the low portion of a number of N bits?
For example, for an 8 bit number, get the least significant 4 bits, for a 16 bit number, get the least significant 8 bits.
To get the lower half of a built-in integer type you can try something like this:
#include <iostream>
#include <climits>
using std::cout;
using std::endl;
template<typename T>
constexpr T lowbits(T v) {
return v & (T(1) << CHAR_BIT * sizeof v / 2) - 1;
}
int main() {
cout << std::hex << (int)lowbits<int8_t>(0xde) << endl; // will print e
cout << std::hex << lowbits<int16_t>(0xdead) << endl; // will print ad
cout << std::hex << lowbits<int32_t>(0xdeadbeef) << endl; // will print beef
cout << std::hex << lowbits<int64_t>(0xbeefdeaddeadbeef) << endl; // will print deadbeef
}
Note that
return v & (T(1) << CHAR_BIT * sizeof v / 2) - 1;
is equivalent to:
return v & (
(static_cast<T>(1)
<<
(CHAR_BIT * (sizeof v) / 2)) // number of bits divided by 2
- 1
);
In essence you are creating a bit-mask (simply another integer) that has 0-bits for all higher bits and 1-bits for all lower bits.
If an integer type has N bits this is done by shifting a 1-bit into the Nth position and then subtracting 1 from it. The subtraction has the result that all bits below the 1 will be set.
And-ing this with the given value yields only the lower half of the value v.
You can easily generalize this approach to retrieving any number of lower bits by replacing CHAR_BIT * sizeof v/2 with the number of bits you want to retrieve.
To get only the higher bits you can simply negate the resulting mask using the ~ operator.
If you require arbitrary sized integers you can try finding the equivalent operations for this procedure in the GNU gmp library.
Let us define a variable called mask which is the pattern to mask off (or retain) some bits. The operation to get the least significant bits is:
result = value & mask;
For an example, test with value == 13 and mask == 7.
This works will all POD types, except for floating point. The least significant Q bits of a floating point, doesn't make sense (unless you really need to do this).
If you have no need for more bits than the largest internal integral type, you could use something like this:
template <typename T>
T low_bits(T data, size_t bit_count)
{
T mask = (1U << bit_count) - 1U;
return value & mask;
}
For a non-template solution, one could use a macro:
#define LOW_BITS(value, bit_count) \
(value & ((1U << bit_count) - 1U))
This lets the compiler figure out the code based on the data type of value.
A macro form of the expression: value & mask.
The thorn or issue comes into play when N > sizeof(*largest type*). In this case, the number can't be represented by internal data types, so one has to come up with a different solution.
The solution for N-bit depends on whether the multi-byte representation of the number is Big Endian or Little Endian. For Big Endian platforms, the least significant value will be at highest address, while on Little Endian platforms, the least significant is at the lowest address.
The solution I'm proposing treats the N-bit number as an array of bytes. A byte contains 8-bits (on most platforms), and bytes can be masked differently than multibyte quantities.
Here's the algorithm:
1. Copy the least significant bytes that are completely masked to the result variable.
2. Mask the next largest byte and copy result byte to result number.
3. Pad remaining bytes with 0.
As far as the function parameters go, you'll need:
1) Pointer to the memory location of the original number.
2) Pointer to the result number.
3) Pointer to the mask.
4) Size of the number, in bytes.
The algorithm can handle N-bit numbers, limited by the amount of memory on the platform.
Note: sorry about not providing code, but I need to get back to work. :-(

Combining 2 Hex Values Into 1 Hex Value

I have a coordinate pair of values that each range from [0,15]. For now I can use an unsigned, however since 16 x 16 = 256 total possible coordinate locations, this also represents all the binary and hex values of 1 byte. So to keep memory compact I'm starting to prefer the idea of using a BYTE or an unsigned char. What I want to do with this coordinate pair is this:
Let's say we have a coordinate pair with the hex value [0x05,0x0C], I would like the final value to be 0x5C. I would also like to do the reverse as well, but I think I've already found an answer with a solution to the reverse. I was thinking on the lines of using & or | however, I'm missing something for I'm not getting the correct values.
However as I was typing this and looking at the reverse of this: this is what I came up with and it appears to be working.
byte a = 0x04;
byte b = 0x0C;
byte c = (a << 4) | b;
std::cout << +c;
And the value that is printing is 76; which converted to hex is 0x4C.
Since I have figured out the calculation for this, is there a more efficient way?
EDIT
After doing some testing the operation to combine the initial two is giving me the correct value, however when I'm doing the reverse operation as such:
byte example = c;
byte nibble1 = 0x0F & example;
byte nibble2 = (0xF0 & example) >> 4;
std::cout << +nibble1 << " " << +nibble2 << std::endl;
It is printout 12 4. Is this correct or should this be a concern? If worst comes to worst I can rename the values to indicate which coordinate value they are.
EDIT
After thinking about this for a little bit and from some of the suggestions I had to modify the reverse operation to this:
byte example = c;
byte nibble1 = (0xF0 & example) >> 4;
byte nibble2 = (0x0F & example);
std:cout << +nibble1 << " " << +nibble2 << std::endl;
And this prints out 4 12 which is the correct order of what I am looking for!
First of all, be careful about there are in fact 17 values in the range 0..16. Your values are probably 0..15, because if they actually range both from 0 to 16, you won't be able to uniquely store every possible coordinate pair into a single byte.
The code extract you submitted is pretty efficient, you are using bit operators, which are the quickest thing you can ask a processor to do.
For the "reverse" (splitting your byte into two 4-bit values), you are right when thinking about using &. Just apply a 4-bit shift at the right time.

bitwise bitmanipulation puzzle

Hello is have a question for a school assignment i need to :
Read a round number, and with the internal binaire code with bit 0 on the right and bit 7 on the left.
Now i need to change:
bit 0 with bit 7
bit 1 with bit 6
bit 2 with bit 5
bit 3 with bit 4
by example :
if i use hex F703 becomes F7C0
because 03 = 0000 0011 and C0 = 1100 0000
(only the right byte (8 bits) need to be switched.
The lession was about bitmanipulation but i can't find a way to make it correct for al the 16 hexnumbers.
I`am puzzling for a wile now,
i am thinking for using a array for this problem or can someone say that i can be done with only bitwise ^,&,~,<<,>>, opertors ???
Study the following two functions:
bool GetBit(int value, int bit_position)
{
return value & (1 << bit_position);
}
void SetBit(int& value, int bit_position, bool new_bit_value)
{
if (new_bit_value)
value |= (1 << bit_position);
else
value &= ~(1 << bit_position);
}
So now we can read and write arbitrary bits just like an array.
1 << N
gives you:
000...0001000...000
Where the 1 is in the Nth position.
So
1 << 0 == 0000...0000001
1 << 1 == 0000...0000010
1 << 2 == 0000...0000100
1 << 3 == 0000...0001000
...
and so on.
Now what happens if I BINARY AND one of the above numbers with some other number Y?
X = 1 << N
Z = X & Y
What is Z going to look like? Well every bit apart from the Nth is definately going to be 0 isnt it? because those bits are 0 in X.
What will the Nth bit of Z be? It depends on the value of the Nth bit of Y doesn't it? So under what circumstances is Z zero? Precisely when the Nth bit of Y is 0. So by converting Z to a bool we can seperate out the value of the Nth bit of Y. Take another look at the GetBit function above, this is exactly what it is doing.
Now thats reading bits, how do we set a bit? Well if we want to set a bit on we can use BINARY OR with one of the (1 << N) numbers from above:
X = 1 << N
Z = Y | X
What is Z going to be here? Well every bit is going to be the same as Y except the Nth right? And the Nth bit is always going to be 1. So we have set the Nth bit on.
What about setting a bit to zero? What we want to do is take a number like 11111011111 where just the Nth bit is off and then use BINARY AND. To get such a number we just use BINARY NOT:
X = 1 << N // 000010000
W = ~X // 111101111
Z = W & Y
So all the bits in Z apart from the Nth will be copies of Y. The Nth will always be off. So we have effectively set the Nth bit to 0.
Using the above two techniques is how we have implemented SetBit.
So now we can read and write arbitrary bits. Now we can reverse the bits of the number just like it was an array:
int ReverseBits(int input)
{
int output = 0;
for (int i = 0; i < N; i++)
{
bool bit = GetBit(input, i); // read ith bit
SetBit(output, N-i-1, bit); // write (N-i-1)th bit
}
return output;
}
Please make sure you understand all this. Once you have understood this all, please close the page and implement and test them without looking at it.
If you enjoyed this than try some of these:
http://graphics.stanford.edu/~seander/bithacks.html
And/or get this book:
http://www.amazon.com/exec/obidos/ASIN/0201914654/qid%3D1033395248/sr%3D11-1/ref%3Dsr_11_1/104-7035682-9311161
This does one quarter of the job, but I'm not going to give you any more help than that; if you can work out why I said that, then you should be able to fill in the rest of the code.
if ((i ^ (i >> (5 - 2))) & (1 >> 2))
i ^= (1 << 2) | (1 << 5);
Essentially you need to reverse the bit ordering.
We're not going to solve this for you.. but here's a hint:
What if you had a 2-bit value. How would you reverse these bits?
A simple swap would work, right? Think about how to code this swap with operators that are available to you.
Now let's say you had a 4-bit value. How would you reverse these bits?
Could you split it into two 2-bit values, reverse each one, and then swap them? Would that give you the right result? Now code this.
Generalizing that solution to the 8-bit value should be trivial now.
Good luck!