### Thread: inserting 0 and 1 bits

1. No Profile Picture
.
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2002
Posts
296
Rep Power
16

#### inserting 0 and 1 bits

hiyer

how is this normally done? :

say i've got a binary number like:

01011110

and another smaller binary like:

0010

and what i want to do is place the small one into the larger one over-writing, but leaving the external bits as they are? like:

01011110
XXX0010X
=

i can't see how to achieve the X's which are neither 0's or 1's

i can see how to do this if the smaller binary number to insert was made up of entirely 1's or entirely made up of 0's, but not when they're both. is that the answer? do it twice maybe? not sure.

any help much 'preciated :)
2. Maybe I don't understand your question fully, but what exactly do you mean by "place the small one into the larger one over-writing". I thought I'd understood most of it, but then I noticed you're placing an X at the end of 0010, which threw me off completely. Aside from that, you seem to be describing a bitwise and operation (the & operator in C), where you replace all the Xs with 1's and perform a bitwise and to get your desired answer.
3. No Profile Picture
.
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2002
Posts
296
Rep Power
16
i thought the Xs would help explain! :) what i meant by the Xs were nothing. neither zero nor one. masked off.

>you seem to be describing a bitwise and operation (the & operator in C), where you replace all the Xs with 1's and perform a bitwise and to get your desired answer

but bitwise & gives you 0 if the first number is 0 and the second is 1. and then the | gives a 1 if the first number is 1 and second number is 0. all the operations like that are dependent on what's there first - they don't over-write. (the 'second number' here meaning the one i'm adding)

i want to over-write what's there already - so what's there already is irrelevent (irrelevent as far as the part that gets covered by the smaller to be put in number goes)

how to over-write whatever is there with a new smaller binary number, leaving the other parts of the larger binary number untouched, is the question. like:
00101001
X10100XX
01010001
X's representing no change/nothing. - so leaves the first number untouched.
Last edited by balance; January 29th, 2003 at 09:23 AM.
4. No Profile Picture
Contributing User
Devshed God 1st Plane (5500 - 5999 posts)

Join Date
Oct 2000
Location
Back in the real world.
Posts
5,960
Rep Power
194
doing graphics programming? i am not sure if i understand you correctly, but i think you want this:

what it does: clear the bits that are set in the mask, then add the second number.

in your first example, your mask is "00011110". and you have shifted the second value 1 bit to the left (the "X" on the right):

in the second example your mask would be "01111100" and the formula:

5. No Profile Picture
.
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2002
Posts
296
Rep Power
16
thanks m.hirsch. this isn't for graphics programming - i'm just trying to get to grips with bits in general, although i could see it being useful for that certainly.

i think you have understood me but it isn't working! :/ i tried what you suggested, to achieve this:
___________________________________________________
starting number:
10101011

wanted result:
10110111
which is the starting number, but with 101 overwritten, between positions 4 and position 2 (using standard bit numbering (0 being the rightmost bit)) over the starting number
___________________________________________________

this is the code i used:
Code:
```main()
{
unsigned int bin=0xAB;

bin=(bin & !0x1C) | (0x5<<2);

printf("0x%X\n", bin);
}```
0xAB being 10101011
0x1C is 11100
and 0x5 is 101

the above code prints 0x14

0x14 which is 10100, so it took in the correct second number, correctly shifted it over, but the original binary number has entirely dissapeared.

have i misinterpreted what you said or is what you said incorrect? i've tried a few variations on what you said but still can't get it to work. very frustrating :) any ideas? thanks.
Last edited by balance; January 29th, 2003 at 05:38 PM.
6. No Profile Picture
.
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2002
Posts
296
Rep Power
16

#### i got it at last :)

bin=(bin & ~0x1C) | (0x5<<2);

excellent. thanks.
7. No Profile Picture
.
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2002
Posts
296
Rep Power
16

carrying on from that, is there a simpler and shorter way to generate a number with all bits turned on to the required length, than this:

Code:
```#include <stdio.h>
#define LENGTH 4		/* number of 1's required */

main()
{
int i;
int mask=2;			/* make a binary number the right */
for(i=1; i<LENGTH; i++)	/* number of 1's long */

}```
so for example setting length to 3 gives 7 which is 111 represented in binary.

1 = 1 (1)
2 = 3 (11)
3 = 7 (111)
4 = 15 (1111)
5 = 31 (11111) etc...

is that the best way to do it or have i missed something obvious? (obviously i'm talking about the part from int i; to mask--;. not the other parts).

thanks again.
8. No Profile Picture
Contributing User
Devshed God 1st Plane (5500 - 5999 posts)

Join Date
Oct 2000
Location
Back in the real world.
Posts
5,960
Rep Power
194
:o i took the wrong not-operator?! shame on me! back to class!

i << 1 is equal to i*2
i << 2 is equal to i*4

and so on.but it will pad zeros to the right. also using the "<<" operator is significantly faster than multiplying (especially in a loop)
9. No Profile Picture
.
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2002
Posts
296
Rep Power
16
```	int mask;