January 28th, 2003, 09:31 PM

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 overwriting, but leaving the external bits as they are? like:
01011110
XXX0010X
=
01000100 (<< wanted answer)
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 :)
January 28th, 2003, 11:27 PM

Maybe I don't understand your question fully, but what exactly do you mean by "place the small one into the larger one overwriting". 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.
January 29th, 2003, 09:01 AM

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 overwrite. (the 'second number' here meaning the one i'm adding)
i want to overwrite 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 overwrite 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
wanted answer:
01010001
X's representing no change/nothing.  so leaves the first number untouched.
Last edited by balance; January 29th, 2003 at 09:23 AM.
January 29th, 2003, 11:20 AM

doing graphics programming? i am not sure if i understand you correctly, but i think you want this:
newValue=(firstValue & !mask)  secondValue
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):
newValue=(firstValue & !mask)  (secondValue<<1)
in the second example your mask would be "01111100" and the formula:
newValue=(firstValue & !mask)  (secondValue<<2)
January 29th, 2003, 03:15 PM

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.
January 29th, 2003, 05:47 PM

i got it at last :)
bin=(bin & ~0x1C)  (0x5<<2);
excellent. thanks.
January 29th, 2003, 09:18 PM

mask generation  111..
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 */
mask=mask*2;
mask;
printf("%d\n", mask);
}
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.
January 30th, 2003, 01:23 AM

:o i took the wrong notoperator?! 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)
January 30th, 2003, 09:57 AM

so the answer is:
Code:
int mask;
int length=6;
mask=(1<<length)1;
:) i knew they'd be a more concise way than what i had. thanks.