I have a "bitmap" lets say, 64 wide. Meaning 8 bits per Byte. 0000 0000 and let's say I want to activate the second bit, 0100 0000.
I tried making an array of unsigned char and using memcpy.
memcpy(bitmap[2], 1, sizeof(1));
I even tried declaring 1 in a unsigned char variable.
unsigned char bit = 1;
memcpy(bitmap[2], bit, sizeof(bit));
I know that strcpy wont solve anything in this case and memcpy isn't solving it either apparently, there is a proper way to do this for sure. I'm no C expert as you can see..
Maybe I shouldn't be trying to change the value inside the array like this?
let's assume that your bitmap looks like that
unsigned char bitmap[8] = {0};
and you want to activate the second bit (from the left) of the first byte in your bitmap array (bitmap[0]):
bitmap[0] = 1U<<6;
the bitmap[0] binary presentation will be 0100 0000
EDIT
bitmap[i] is 8 bits size.
bitmap[0] = 1<<6; // means 1 shifted to the left with 6 steps
1<<0
0 0 0 0 0 0 0 1
1<<1
0 0 0 0 0 0 1 0
1<<2
0 0 0 0 0 1 0 0
1<<3
0 0 0 0 1 0 0 0
......
1<<6
0 1 0 0 0 0 0 0
Related
I am fairly new to C programming, and I encountered bit masking. What is the general concept and function of bit masking?
Examples are much appreciated.
A mask defines which bits you want to keep, and which bits you want to clear.
Masking is the act of applying a mask to a value. This is accomplished by doing:
Bitwise ANDing in order to extract a subset of the bits in the value
Bitwise ORing in order to set a subset of the bits in the value
Bitwise XORing in order to toggle a subset of the bits in the value
Below is an example of extracting a subset of the bits in the value:
Mask: 00001111b
Value: 01010101b
Applying the mask to the value means that we want to clear the first (higher) 4 bits, and keep the last (lower) 4 bits. Thus we have extracted the lower 4 bits. The result is:
Mask: 00001111b
Value: 01010101b
Result: 00000101b
Masking is implemented using AND, so in C we get:
uint8_t stuff(...) {
uint8_t mask = 0x0f; // 00001111b
uint8_t value = 0x55; // 01010101b
return mask & value;
}
Here is a fairly common use-case: Extracting individual bytes from a larger word. We define the high-order bits in the word as the first byte. We use two operators for this, &, and >> (shift right). This is how we can extract the four bytes from a 32-bit integer:
void more_stuff(uint32_t value) { // Example value: 0x01020304
uint32_t byte1 = (value >> 24); // 0x01020304 >> 24 is 0x01 so
// no masking is necessary
uint32_t byte2 = (value >> 16) & 0xff; // 0x01020304 >> 16 is 0x0102 so
// we must mask to get 0x02
uint32_t byte3 = (value >> 8) & 0xff; // 0x01020304 >> 8 is 0x010203 so
// we must mask to get 0x03
uint32_t byte4 = value & 0xff; // here we only mask, no shifting
// is necessary
...
}
Notice that you could switch the order of the operators above, you could first do the mask, then the shift. The results are the same, but now you would have to use a different mask:
uint32_t byte3 = (value & 0xff00) >> 8;
Masking means to keep, change, or remove a desired part of information. Let’s see an image-masking operation; like this masking operation is removing anything that is not skin:
We are doing an AND operation in this example. There are also other masking operators—OR and XOR.
Bitmasking means imposing mask over bits. Here is a bitmasking with AND—
1 1 1 0 1 1 0 1 input
(&) 0 0 1 1 1 1 0 0 mask
------------------------------
0 0 1 0 1 1 0 0 output
So, only the middle four bits (as these bits are 1 in this mask) remain.
Let’s see this with XOR—
1 1 1 0 1 1 0 1 input
(^) 0 0 1 1 1 1 0 0 mask
------------------------------
1 1 0 1 0 0 0 1 output
Now, the middle four bits are flipped (1 became 0, 0 became 1).
So, using a bitmask, we can access individual bits (examples). Sometimes, this technique may also be used for improving performance. Take this for example-
bool isOdd(int i) {
return i%2;
}
This function tells if an integer is odd/even. We can achieve the same result with more efficiency using a bit-mask—
bool isOdd(int i) {
return i&1;
}
Short Explanation: If the least significant bit of a binary number is 1 then it is odd; for 0 it will be even. So, by doing AND with 1 we are removing all other bits except for the least significant bit, i.e.:
55 -> 0 0 1 1 0 1 1 1 input
(&) 1 -> 0 0 0 0 0 0 0 1 mask
---------------------------------------
1 <- 0 0 0 0 0 0 0 1 output
I want to know how this structure & calloc are allocated in heap. I mean how glibc keep information in memory like this is structure this is its data type so on.
#include<stdio.h>
struct student{
int rollno;
char level;
};
int main(){
struct student *p = calloc(1,sizeof(struct student));
p->rollno=767;
p->level='A';
printf("addr of p is =%p\n",p);
printf("student_struct_size=%ld\n",sizeof(
}
In my system
addr of p is =0x555555756260
(gdb) x/16b 0x555555756260
0x555555756260: -1 2 0 0 65 0 0 0
0x555555756268: 0 0 0 0 0 0 0 0
I can understand why 65 is coming but where is 767, also where is header information about calloc (what is boundary of calloc)
If i Do x/16b 0x555555756260-8 i get 33 , is 33 is size of all payload + header information , can u justify why 33 is coming
(gdb) x/16b 0x555555756260-8
0x555555756258: 33 0 0 0 0 0 0 0
0x555555756260: -1 2 0 0 65 0 0 0
"Where is 767?"
Since you are printing the memory in (signed) bytes, and your int apparently has 4 bytes in little endian and two's complement, you can calculate this by:
least significant byte: (signed byte) -1 = (hex) 0xFF
next significant byte: (signed byte) 2 = (hex) 0x02
next bytes are both zero
4-byte value is 0x000002FF = 767
"Where is header information?"
One possible implementation of memory management stores its management data before the memory block returned to you.
You might want to look in even lower memory bytes. Expect some pointers there.
To understand the entries, you will need to obtain the sources of your library and to study them. Then you might know what the value 33 represents.
where is 767,
-1 and 2 are 767, little endian. -1 + 2 * 256 = 0xff + 2 * 256 = 255 + 2*256 = 767.
where is header information about calloc
Those 8 bytes with 33 are all *alloc needs, there's nothing really more.
can u justify why 33 is coming
From glibc/malloc.c:
| Size of chunk, in bytes |A|M|P|
33 is 0b100001:
100001
AMP
^^ <- SIZE
Bit P is set, which means Previous chunk is used.
Bit M is not set, which means the region is not Mmap()-ed.
Bit A is not set, which means the chunk is in the main Area.
The rest is size. 0b100000 is 32 - the allocated chunk has 32 bytes. 8 bytes for a size_t number to store that 33 and malloc_usable_size will return 32 - 8 = 24.
I am fairly new to C programming, and I encountered bit masking. What is the general concept and function of bit masking?
Examples are much appreciated.
A mask defines which bits you want to keep, and which bits you want to clear.
Masking is the act of applying a mask to a value. This is accomplished by doing:
Bitwise ANDing in order to extract a subset of the bits in the value
Bitwise ORing in order to set a subset of the bits in the value
Bitwise XORing in order to toggle a subset of the bits in the value
Below is an example of extracting a subset of the bits in the value:
Mask: 00001111b
Value: 01010101b
Applying the mask to the value means that we want to clear the first (higher) 4 bits, and keep the last (lower) 4 bits. Thus we have extracted the lower 4 bits. The result is:
Mask: 00001111b
Value: 01010101b
Result: 00000101b
Masking is implemented using AND, so in C we get:
uint8_t stuff(...) {
uint8_t mask = 0x0f; // 00001111b
uint8_t value = 0x55; // 01010101b
return mask & value;
}
Here is a fairly common use-case: Extracting individual bytes from a larger word. We define the high-order bits in the word as the first byte. We use two operators for this, &, and >> (shift right). This is how we can extract the four bytes from a 32-bit integer:
void more_stuff(uint32_t value) { // Example value: 0x01020304
uint32_t byte1 = (value >> 24); // 0x01020304 >> 24 is 0x01 so
// no masking is necessary
uint32_t byte2 = (value >> 16) & 0xff; // 0x01020304 >> 16 is 0x0102 so
// we must mask to get 0x02
uint32_t byte3 = (value >> 8) & 0xff; // 0x01020304 >> 8 is 0x010203 so
// we must mask to get 0x03
uint32_t byte4 = value & 0xff; // here we only mask, no shifting
// is necessary
...
}
Notice that you could switch the order of the operators above, you could first do the mask, then the shift. The results are the same, but now you would have to use a different mask:
uint32_t byte3 = (value & 0xff00) >> 8;
Masking means to keep, change, or remove a desired part of information. Let’s see an image-masking operation; like this masking operation is removing anything that is not skin:
We are doing an AND operation in this example. There are also other masking operators—OR and XOR.
Bitmasking means imposing mask over bits. Here is a bitmasking with AND—
1 1 1 0 1 1 0 1 input
(&) 0 0 1 1 1 1 0 0 mask
------------------------------
0 0 1 0 1 1 0 0 output
So, only the middle four bits (as these bits are 1 in this mask) remain.
Let’s see this with XOR—
1 1 1 0 1 1 0 1 input
(^) 0 0 1 1 1 1 0 0 mask
------------------------------
1 1 0 1 0 0 0 1 output
Now, the middle four bits are flipped (1 became 0, 0 became 1).
So, using a bitmask, we can access individual bits (examples). Sometimes, this technique may also be used for improving performance. Take this for example-
bool isOdd(int i) {
return i%2;
}
This function tells if an integer is odd/even. We can achieve the same result with more efficiency using a bit-mask—
bool isOdd(int i) {
return i&1;
}
Short Explanation: If the least significant bit of a binary number is 1 then it is odd; for 0 it will be even. So, by doing AND with 1 we are removing all other bits except for the least significant bit, i.e.:
55 -> 0 0 1 1 0 1 1 1 input
(&) 1 -> 0 0 0 0 0 0 0 1 mask
---------------------------------------
1 <- 0 0 0 0 0 0 0 1 output
I represent a square of 1s and 0s with an integer type variable. Here's what it might look like:
1 1 1 0
0 1 0 0
0 0 0 0
0 0 0 0
The corresponding integer is coded from the bottom right corner to the top left, and the top left corner corresponds to 2^0. An integer representation of this grid is: 0000 0000 0010 0111 (2) = 39(10)
The grid is always a square with known width. In my code, I need to increment the width by 1 a lot of times, thus changing the map to:
1 1 1 0 0
0 1 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
Which is equivalent to inserting a 0 every x = width bits (width = 4 in the example). Overflows can't happen because the size of the integer is much larger than required.
In C, how can I insert a zero every x bits?
Edit1: since this operation will be happening a lot of times on numerous grids, loops are to be avoided.
Edit2: the reason I use this packing is because I do bitwise operations between grids to search for combinations.
Look at the problem in the way you would look as adding 0 to every forth position in decimal number. What you would do in decimal is:
1 513 650 -> 105 130 650
You can see, that your are just preserving the numbers less than 10^3 and multiplying the rest by 10. Than preserving the number less than 10^7 (not 10^6, because you already multiplied) and multypling the right side by 10....
It works same way for binary.
#include <iostream>
#include <cmath>
int power(int x, int n){
return n == 1 ? x : x*power(x,n-1);
}
int main(int argc, char const *argv[])
{
int n = 4;
int grid = 1024;
int grid_len = std::floor(std::log2(grid)) ; // what is the position of last 1, = how long i need to insert 0
for (int steps = 0; steps*n < grid_len; ++steps)
{
std::cout << steps << std::endl;
int preserve = grid % power(2,n*(steps+1) );
grid = (grid - preserve) * 2;
grid += preserve;
}
std::cout << grid << std::endl;
return 0;
}
The code prints 71, which in binary is 1000111, so it works on example you provided.
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 8 years ago.
Improve this question
The "length" least significant bits of one address shall be compared with the "length" least significant bits of another address.
Can some one help me in getting the best optimal solution for the same?
Example:
address1 = 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 1 0 0 1
address2 = 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 1 1 0 0 1
If the comparison bits are 00100 then the above two addresses are same.
Apparently, you want to compare N least-significant bits. In that case your N is not a mask. "Mask" is an established term with very specific meaning. Your N is not a mask. It is just number of bits you have to compare.
To achieve that, you can start with actually generating a real mask from your N. This
uintptr_t mask = 1;
mask = (mask << N) - 1;
will create a mask that has 1s in N least-significant binary positions. (I don't know what type you use to store your addresses. That's the type you should use in place of uintptr_t.)
Then you can use that mask to compare your addresses
(address1 & mask) == (address2 & mask)
or
((address1 ^ address2) & mask) == 0
Alternatively, you can solve the same problem without using any masks at all. If your addresses have M bits total, then the comparison can be expressed as
(address1 << (M - N)) == (address2 << (M - N))
As #AndreyT wrote, you really are not describing a mask.
Trying to guess what you really do mean, here's my best attempt:
(assuming your int-size is 32-bits. You can convert to sizeof() or use 64 as appropriate)
(untested, but reasonably well commented, so you can test and fix it yourself)
void Compare(int addrA, int addrB, int nBits)
{
// This is all 1s in binary.
unsigned int mask = 0xFFFFFFFF;
mask = mask >> (32-nBits);
// Example: if nBits = 4
// then 32-4 = 28 and
// Mask == [1111] >> 28 == 28-zeros... followed by 4 Ones
if ((addrA & mask) == (addrB & mask))
{
printf("Same\n");
} else
{
printf("Not the same\n");
}
}
Sample Call
int main(void)
{
int address1 = 0x2A09; // 00000000 0010 1010 0000 1001
int address2 = 0x2A19; // 00000000 0010 1010 0001 1001
Compare(address1, address2, 4);
}
to turn the number 4 (or 00100) into the mask you want, you need
#define MASK(n) ((1 << (n)) - 1)
Also, if you are going to quote a binary representation of an integer, in C use 0b100, then we know you mean 4. 00100 is an octal constant.