LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-16-2009, 06:35 PM   #1
charlitos
Member
 
Registered: Feb 2009
Posts: 51

Rep: Reputation: 16
C - Base 4 addition


Im trying to do an exercise I found on the internet(classes are over and Im bored) to generate a dna sequence using different bases.

base 1

A C G T

base 2

AA AC AG AT CA CC CG CT GA GC GG GT TA TC TG TT

base 3

AAA AAC AAG AAT ACA ACC ACG ACT ....


I thought it would be better converting letters into numbers:

A = 1
C = 2
G = 3
T = 4

and the sequence would be

base 1:
1 2 3 4

base 2:
11 12 13 14 21 22 23 24 31 32 33 34 41 42 43 44

base 3:
111 112 113 114 121 122 123 124 131 132 133 134 141 142 143 144 211...

this way I thought that if using base for addition (+ 1) I can always get the next group of numbers(letters) with basically no effort compared to the alternative of generating n loops( n = base) to get the whole sequencing.

The thing is I need a function to do base four addition, I could also start doing it myself but I was wondering if theres somethign already made I can use for this specific exercise.

Also I would appreciate any other ideas to find better ways to solve this exercise.

thanks in advance.

Last edited by charlitos; 04-16-2009 at 11:52 PM.
 
Old 04-16-2009, 07:56 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
Start from 0, not 1. Decimal goes from 0 to 9, octal from 0 to 7, and hexadecimal from 0 to f (15). Also, 4 is a power of 2, which means it has the same niceties as octal and hexadecimal (octal is 3 bits grouped, hexadecimal is 4 bits grouped).
 
Old 04-16-2009, 09:48 PM   #3
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,162

Rep: Reputation: 504Reputation: 504Reputation: 504Reputation: 504Reputation: 504Reputation: 504
Like tuxdev said, it is better to use 0,1,2,3.

Then what he says about base 4 being nice, he means that you can easily convert to a base 4 number representation.

Consider (for example) a 3-digit base 4 number:

123

In hex representation that is: 0x1B, bin: 01 1011

Add one to that number, using your normal '+' function: you get 0x1C (01 1100)

Convert that back to base 4:

130

Now because number are represented binary in your computer, the easiest way to convert hex to quaternary is:

Code:
nr_digits=0;
repeat
  digit = new_number & 0x3 //leaves only the rightmost bits
  new_number = new_number shr 2 //move to the next digit
  nr_digits++
until nr_digits >= max_digits;
How to string all those digits together I leave happily to you. Above code is not syntactically correct but you get the idea.

Any chance to implement those DNA strings and create a dino?

jlinkels
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Redhat base and Dedian base linux??? nw53 Linux - Newbie 6 04-29-2007 01:16 PM
new addition, old reader vicious1 LinuxQuestions.org Member Intro 1 04-12-2007 05:33 PM
Second Hard Drive Addition.... Pleiades Fedora 1 04-15-2004 08:39 PM
Addition with matching arrays (or something) slakmagik Programming 2 02-25-2004 09:52 PM
Links Addition(s) MasterC LQ Suggestions & Feedback 2 03-24-2003 01:42 PM


All times are GMT -5. The time now is 01:57 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration