bit-manipulation

Detecting single one-bit streams within an integer

非 Y 不嫁゛ 提交于 2019-12-12 22:00:19
问题 I have to check a number if it satisfies the following criteria: in binary, all one-bits must be successive. the number must have at least one bit set. the successive one-bits may start at the MSB or end at the LSB, so it's perfectly valid if the number is made up of a single one-bit stream followed by a zero-bit stream or vice versa. I wrote a code that checks for these conditions for a real-world problem (checking data-file integrity). It works without problems and it's anything but time

What is the rationale behind (x % 64) == (x & 63)? [duplicate]

我是研究僧i 提交于 2019-12-12 21:01:33
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Bitwise and in place of modulus operator Can someone explain the rationale that makes both expressions equivalents? I know it only works because 64 is a power of two, but how can I logically or mathematically go from division to bitwise and? 回答1: The operation x % 64 returns the remainder when x is divided by 64, which (assuming x>0) must be a number between 0 and 63. Let's look at this in binary: 63 dec = 0011

Trying to understand Bitmap.Config.ARBG_8888

和自甴很熟 提交于 2019-12-12 19:18:51
问题 I'm trying to understand packing of color bytes for ARGB_8888 format. The documentation states that packing should be done using this formula: int color = (A & 0xff) << 24 | (B & 0xff) << 16 | (G & 0xff) << 8 | (R & 0xff); But shouldn't it be instead: int color = (A & 0xff) << 24 | (R & 0xff) << 16 | (G & 0xff) << 8 | (B & 0xff); When I unpack a sample pixel from a ARGB_8888 color encoded bitmap that has all red pixels, I'm using: final int r = (p >> 16) & 0xff; final int g = (p >> 8) & 0xff;

Getting Lower-Order N Bits

北慕城南 提交于 2019-12-12 18:25:47
问题 Is there any way you can get any integer's lower-order n bits (where n can be any number between 1 and 32) without previously pre-computing 32 bitmasks, one for each order, and using the & operator? I also don't want to be using % with powers of two, just bitwise operations. Edit: Say, for example, that a user enters an integer Num and another integer ShiftCount with a value ranging from 1 to 32. I want to store in a third variable the bits that are lost in the operation Num >> ShiftCount .

What is good way to negate an integer in binary operation in python?

那年仲夏 提交于 2019-12-12 18:04:15
问题 Based on what I've read about the binary representation of integers, the first bit is for sign (positive or negative). Let's say we have an integer x = 5 and sys.getsizeof(x) returns 28 (that is binary representation in 28 bit). For now I am trying to flip the first bit to 1 by using x|=(1<<27) but it returns 134217733 . I was just wondering whether it needs to be some negative number? (not -5) Is there anything wrong with what I am doing? 回答1: You can't switch a Python int from positive to

Bit functions in Think Pascal

蓝咒 提交于 2019-12-12 16:29:04
问题 I am converting an old program written on Mac Think Pascal to Pascal on Windows. Think Pascal has functions for bit manipulations, such as btst(n, i : longint) : boolean bitxor(i, j : longint) : longint bclr(n, i : longint) : longint bset(n, i : longint) : longint ...and others. If you know what these functions do, please help. I have found some related information, for example "Porting to GNU Pascal from traditional Macintosh Pascal compilers". This document has implementations of the bit

Setting individual bits in mongo, to store a bitmask

跟風遠走 提交于 2019-12-12 16:08:14
问题 I want to store some flags into a mongo db. For now I have the following: > db.test.save({a:0x1}) > db.test.save({a:0x3}) > db.test.save({a:0x2}) > db.test.save({a:0x2}) > db.test.save({a:0x4}) > db.test.save({a:0x5}) > db.test.find({'$where': "this.a & 0x1"}) Is there a more effective way? 回答1: While you can do it that way, I'd suggest using separate boolean fields for each flag. That will take up more space but will be faster to query because it won't use javascript and can use indexes if

Fastest and most efficient way to find the maximum no. that can be obtained by performing bitwise and on 2 DISTINCT elements of array

时光总嘲笑我的痴心妄想 提交于 2019-12-12 14:00:53
问题 Given an array of non-negative integers, what is the fastest and most efficient way to find the maximum no. that can be obtained by performing bitwise and (i.e, & operator) on 2 DISTINCT elements of the array? This is my code until now : max = 0 for(i=0; i<n; i++) { for(j=i+1; j<n; j++) { temp = a[i] & a[j]; if(temp > max) max = temp } } This, of course, is the naive method. I am looking for a more efficient solution. Maybe something like using a trie(actually a binary tree) to find max XOR

Gather bits at specific positions into a new value

早过忘川 提交于 2019-12-12 13:49:03
问题 I have a bit-mask of N chars in size, which is statically known (i.e. can be calculated at compile time, but it's not a single constant, so I can't just write it down), with bits set to 1 denoting the "wanted" bits. And I have a value of the same size, which is only known at runtime. I want to collect the "wanted" bits from that value, in order, into the beginning of a new value. For simplicity's sake let's assume the number of wanted bits is <= 32. Completely unoptimized reference code which

signed byte type and bitwise operators in Java?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 11:03:47
问题 quoting from oracle website "byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive)". here, the first two lines are valid but the last is not byte b = -128; byte b1 = 127; byte b2 = b>>>b1;//illegal Q1) what is meant exactly by 8-bit signed? 128 in binary format would be 1000 0000 and -128 would need an extra bit for the negative sign, where it would fit if all the 8 bits are occupied. Q2) for int, there is a