C code 1ULL and bit calculation


 
Thread Tools Search this Thread
Top Forums Programming C code 1ULL and bit calculation
# 1  
Old 06-13-2013
C code 1ULL and bit calculation

I found this block of C code to create combinations of A, T, C & G characters (DNA bases). Can anybody explain this code for me, especially with 1ULL<< and [y & 3]?
Code:
16  for (x = 0; x < 1ULL << (2 * k); ++x)
17    {
18      for (i = 0, y = x; i < k; ++i, y >>= 2)
19      putchar ("ACGT"[y & 3]);
20    }

Thanks a lot!
# 2  
Old 06-13-2013
1ULL is 'unsigned long long'. This is probably intended to be an unsigned, 64-bit number. They make sure it's 64-bit to avoid accidentally overflowing the number of (1<<(2*k)) for high values of k.

"y & 3" masks off all but the three two lowest bits of y, making it effectively "y % 8 4", which would produce a modulous between 0 and 7 3.

Doy, thanks Alister.

Last edited by Corona688; 06-13-2013 at 03:53 PM..
# 3  
Old 06-13-2013
Quote:
Originally Posted by Corona688
"y & 3" masks off all but the three lowest bits of y
I'm sure its just a momentary lapse, but 3 (11 base 2) only passes the 2 least significant bits.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 4  
Old 06-13-2013
Thanks Corona & Alister!

k is the arugment from stdin. Say k =7, can you walk me thru the two loops
Code:
1ULL << (2 * k) 
 y >>= 2

and the line
Code:
putchar("ACGT[y & 3]")?

I am trying to catch C at this moment, barely have ideas about bitwise operations.
Thanks again!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Windows & DOS: Issues & Discussions

Which version of Windows Vista to install with a product key? 32-bit or 64-bit?

Hello everyone. I bought a dell laptop (XPS M1330) online which came without a hard drive. There is a Windows Vista Ultimate OEMAct sticker with product key at the bottom case. I checked dell website (here) for this model and it says this model supports both 32 and 64-bit version of Windows... (4 Replies)
Discussion started by: milhan
4 Replies

2. Programming

64 bit code debugging using gdb and ddd

Hello I have built our application on AIX 7.1 as a 64 bit application. My queries are as follows: Can a 32bit gdb (v7.6) and ddd (data display debugger - v3.3.12), debug a 64bit executable ? If I have a small 64bit a.exe executable that seems to work. If I have a more complicated executable... (4 Replies)
Discussion started by: biju64
4 Replies

3. UNIX for Dummies Questions & Answers

Trying to understand a complex bit of code

Hi, To re-introduce myself, I'm a router guy trying to learn some scripting from the examples in my work place... In a ksh script, one of the script guys wrote the following and I am trying to understand it. I'm hoping someone can explain it to me. The script flow enters a case structure.... (5 Replies)
Discussion started by: Marc G
5 Replies

4. Shell Programming and Scripting

How to handle 64 bit arithmetic operation at 32 bit compiled perl interpreter?H

Hi, Here is the issue. From the program snippet I have Base: 0x1800000000, Size: 0x3FFE7FFFFFFFF which are of 40 and 56 bits. SO I used use bignum to do the math but summing them up I always failed having correct result. perl interpreter info, perl, v5.8.8 built for... (0 Replies)
Discussion started by: rrd1986
0 Replies

5. Shell Programming and Scripting

Learning - Why does this bit of code fail

In bash: for i in 1 2 3 4 5 do tmp_${i}=$i echo $i done It gives error on executing tmp_1=1, and so on... Why? If I executed this in prompt, not from script, it shouldn't make a subshell should it? please use "code" tags! thanks... (2 Replies)
Discussion started by: StuartH
2 Replies

6. Solaris

Porting C++ 32-bit code on 64-bit Solaris

Hi, I am trying to convert 32-bit code to 64-bit. I have defined function int main() { int* l; size_t len1; fun(len1); return 0; } void fun(int* ptr) { cout<<"\nsizeof(ptr)"<<sizeof(ptr); } However while compiling getting error as : Error: Formal argument ptr... (2 Replies)
Discussion started by: amit_27
2 Replies

7. Programming

Bad Mortgage Calculation Code

Hello, I'm teaching myself C and attempting to write a little program that will calculate the monthly payments on a fixed-rate mortage: /* Aaron's mortgage foo */ // mortgage.c #include <stdio.h> #include <math.h> int main(void) { float interestRate; // The bank's fixed... (6 Replies)
Discussion started by: Aaron Van
6 Replies

8. UNIX for Advanced & Expert Users

Migrating perl code from 32 to 64 bit OS

Hello all, I am migrating perl code from 32-bit Solaris 2.5 to 64-bit Solaris 9. On which part of code should I concentrate? And please suggest if there is any checklist for the same. Thanks in advance, Devesh. (3 Replies)
Discussion started by: devesh
3 Replies

9. Programming

32-bit code with gcc

I want to create a 32 bit shared library. I am using following commands 1. gcc -c test1.c 2. ld -b test1.o -o test1.sl But this creates 64 bit shared library. Unix version of the server on which I am working is HP-UX 11.11 (1 Reply)
Discussion started by: amol.chavan
1 Replies
Login or Register to Ask a Question