SunQuest
           C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old January 28th, 2003, 09:31 PM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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
=
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

Reply With Quote
  #2  
Old January 28th, 2003, 11:27 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 h 55 m 33 sec
Reputation Power: 797
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.

Reply With Quote
  #3  
Old January 29th, 2003, 09:01 AM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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
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.

Reply With Quote
  #4  
Old January 29th, 2003, 11:20 AM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184
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)
__________________
--
Manuel Hirsch - Linux, FreeBSD, programming, administration articles, tutorials and more.

Reply With Quote
  #5  
Old January 29th, 2003, 03:15 PM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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.

Reply With Quote
  #6  
Old January 29th, 2003, 05:47 PM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
i got it at last :)

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

excellent. thanks.

Reply With Quote
  #7  
Old January 29th, 2003, 09:18 PM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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.

Reply With Quote
  #8  
Old January 30th, 2003, 01:23 AM
M.Hirsch M.Hirsch is offline
Contributing User
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Oct 2000
Location: Back in the real world.
Posts: 5,969 M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level)M.Hirsch User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 39 m 55 sec
Reputation Power: 184
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)

Reply With Quote
  #9  
Old January 30th, 2003, 09:57 AM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > inserting 0 and 1 bits


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway