January 28th, 2003, 10:31 PM
inserting 0 and 1 bits
how is this normally done? :
say i've got a binary number like:
and another smaller binary like:
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:
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 29th, 2003, 12:27 AM
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.
January 29th, 2003, 10: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 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:
X's representing no change/nothing. - so leaves the first number untouched.
Last edited by balance; January 29th, 2003 at 10:23 AM.
January 29th, 2003, 12:20 PM
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, 04: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:
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:
0xAB being 10101011
unsigned int bin=0xAB;
bin=(bin & !0x1C) | (0x5<<2);
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 06:38 PM.
January 29th, 2003, 06:47 PM
i got it at last :)
bin=(bin & ~0x1C) | (0x5<<2);
January 29th, 2003, 10: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:
so for example setting length to 3 gives 7 which is 111 represented in binary.
#define LENGTH 4 /* number of 1's required */
int mask=2; /* make a binary number the right */
for(i=1; i<LENGTH; i++) /* number of 1's long */
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).
January 30th, 2003, 02:23 AM
: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)
January 30th, 2003, 10:57 AM
so the answer is:
:) i knew they'd be a more concise way than what i had. thanks.