bit-manipulation

Bitshift and integer promotion?

Normally, C requires that a binary operator's operands are promoted to the type of the higher-ranking operand. This can be exploited to avoid filling code with verbose casts, for example: if (x-48U<10) ... y = x+0ULL << 40; etc. However, I've found that, at least with gcc, this behavior does not work for bitshifts. I.e. int x = 1; u...

Optimize me! (C, performance) -- followup to bit-twiddling question

Thanks to some very helpful stackOverflow users at Bit twiddling: which bit is set?, I have constructed my function (posted at the end of the question). Any suggestions -- even small suggestions -- would be appreciated. Hopefully it will make my code better, but at the least it should teach me something. :) Overview This function wil...

strange C++ warning

certain compiler, derived from EDG gives me expression has no effect warning on this line return 1 << ((i == j) + (k == l) + ((i == k) & (j == l))); values are runtime values, compiler does not know them. return 1 << (((i == k) & (j == l))); // no warning here return 1 << ((i == j) + (k == l)); //or here am I missing something or i...

Is this checksum calculation completely waterproof?

long make_checksum(const char* str) { long chk=0; long rot=0; while(*str) { rot<<=9; rot|=(rot>>23); rot^=*(char*)str++; chk+=rot; } return chk; } Not waterproof means: there's a chance I can get the same checksum for two different strings. ...

calculating the number of bits using K&R method with infinite memory

I got answer for the question, counting number of sets bits from here. http://stackoverflow.com/questions/109023/best-algorithm-to-count-the-number-of-set-bits-in-a-32-bit-integer long count_bits(long n) { unsigned int c; // c accumulates the total bits set in v for (c = 0; n; c++) n &= n - 1; // clear the least signif...

Choosing values for constants

One thing I've never really understood is why in many libraries, constants are defined like this: public static final int DM_FILL_BACKGROUND = 0x2; public static final int DM_FILL_PREVIOUS = 0x3; public static final int TRANSPARENCY_MASK = 1 << 1; public static final int TRANSPARENCY_PIXEL = 1 << 2; What's up with the 0x and << stuff?...

Bit Flags - What am I missing?

a simple code: protected void Page_Load(object sender, EventArgs e) { DateTime now = DateTime.UtcNow; lit.Text += "<br/>now.DayOfWeek: " + now.DayOfWeek.ToString(); // weekdays (Saturday is not included) DayOfWeek runningDays = DayOfWeek.Monday | DayOfWeek.Tuesday | DayOfWeek.Wednesday | DayOfWeek.Thursday | DayOfWeek....

How to convert an integer to variable length byte string?

I want to convert an integer (int or long) a big-endian byte string. The byte string has to be of variable length, so that only the minimum number of bytes are used (the total length length of the preceding data is known, so the variable length can be inferred). My current solution is import bitstring bitstring.BitString(hex=hex(456))...

How do you "concatenate" two 32 bits int to get a 64 bits long in Python?

I want to generate 64 bits long int to serve as unique ID's for documents. One idea is to combine the user's ID, which is a 32 bit int, with the Unix timestamp, which is another 32 bits int, to form an unique 64 bits long integer. A scaled-down example would be: Combine two 4-bit numbers 0010 and 0101 to form the 8-bit number 00100101...

What's the faster way to implement the signed-to-unsigned map r(x)= 2x-1 if x>0 and r(x) = 2x otherwise

Mapping signed to unsigned integers bijectively can be done using common techniques like Two's complement. Unfortunately, they fail to map small negative integers to small numbers. For compression algorithms, we often want to preserve the absolute value of numbers as much as possible: small negative and positive numbers must be mapped to...

How do uppercase and lowercase letters differ by only one bit?

I have found one example in Data and Communication Networking book written by Behrouza Forouzan regarding upper- and lowercase letters which differ by only one bit in the 7 bit code. For example, character A is 1000001 (0x41) and character a is 1100001 (0x61).The difference is in bit 6, which is 0 in uppercase letters and 1 in lowercase...

I don't understand the following C code line

I found the following thread: http://stackoverflow.com/questions/777617/calculate-broadcast-address-from-ip-and-subnet-mask and there the link to http://lpccomp.bc.ca/netmask/netmask.c Could someone please explain the following line, I don't understand: for ( maskbits=32 ; (mask & (1L<<(32-maskbits))) == 0 ; maskbits-- ) especially...

Is there a more efficient way to get the length of a 32bit integer in bytes?

I'd like a shortcut for the following little function, where performance is very important (the function is called more than 10.000.000 times): inline int len(uint32 val) { if(val <= 0x000000ff) return 1; if(val <= 0x0000ffff) return 2; if(val <= 0x00ffffff) return 3; return 4; } Does anyone have any idea... a cool bi...

best book for bit/binary manipulation

I've looked at some interview questions and they required heavy bit manipulations. Could anyone please recommend a good book that would allow me to polish my skills in bit manipulation ? (from beginner to intermediate/advanced) with good exercises to pratice ? ...

What's the fastest way to represent and multiply sparse boolean matrices?

So, I'm using boolean matrices whose dimension is usually a couple dozen to a couple hundred, they are usually quite sparse (just some 2-4 non-zeros in most rows and columns) and my runtime is heavily dominated by their multiplication. What datastructure suits best for speeding up multiplication under these circumstances? Currently I'm...

what can you do with just binary or ternary raster operations ?

What soft of bit twiddling can and can't you do using binary or ternary raster arithmetic with blitting functions. Some more examples of bit twiddling: avoiding branches in chess programming, hacker's delight ...

how are bitshift, bitrotate implemented in circuit ?

Can you implement bit shift using only logic operations: and, or, not, xor ? Can you use bitshift in a bitblt? ...

C Read char as binary

This is actually part of a project I'm working on using an avr. I'm interfacing via twi with a DS1307 real-time clock IC. It reports information back as a series of 8 chars. It returns in the format: // Second : ds1307[0] // Minute : ds1307[1] // Hour : ds1307[2] // Day : ds1307[3] // Date : ds1307[4] // Month : ds1307[5] // Yea...

toggle a bit at ith positon

Possible Duplicate: How do you set, clear and toggle a single bit in C? Can some one help me how to toggle a bit at ith position. One way is to ((n>>i)^1) < < i. Are there any other ways ? ...

Logical NOT (!) operator won't work with bitwise statement

I am attempting to determine if I can compute the sum of two 32 bit integers without overflow, while making use of only certain bitwise and other operators. So, if the integers x and y can be added without overflow, the following code should return 1, and 0 otherwise. (((((x >> 31) + (y >> 31)) & 2) >> 1)) However, it returns 0 when ...