bit-manipulation

What is the most efficient way to zero all bits below the most significant set bit?

柔情痞子 提交于 2019-12-23 06:41:29
问题 So for the following sequence: 0001000111000 The desired result would be: 0001000000000 I am fully aware that this is achievable by finding the MSB's index with assembly BSRL (or similar bit-twiddling hack) then >> bit shifting the number by (index - 1), then << shifting back by (index - 1), but I want to know whether there is, specifically, an assembly instruction or a sequence of instructions with better performance, not a bit-twiddling hack that can do this. 回答1: There's no single

Basic Android code - EditText's InputType - why was the bitwise operator used?

六眼飞鱼酱① 提交于 2019-12-23 06:13:41
问题 I'm working my way through a bunch of Android tutorials on YouTube, I expect they are a bit out of date by now. The bit I'm on currently has the following code: tglButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (tglButton.isChecked()) { inputText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); } else { inputText.setInputType(InputType.TYPE_CLASS_TEXT); } } }); The video glazes over why the bitwise operator is used here

2s compliment general form

ⅰ亾dé卋堺 提交于 2019-12-23 05:51:19
问题 Suppose I have a system that has integers that work in mod n . So with my integers adding one to n-1 is actually equal to zero. Suppose further that you define the twos compliment of a number to be the number when added to itself is equal to zero. That is: x + C(x) = 0 (here C(x) is the twos compliment of x) What in general should I do to get the twos compliment of x? The real problem: If x is a number in binary I could just invert all of the the bits of x and then add one to that number.

Bit manipulation and string message structure with xslt

自古美人都是妖i 提交于 2019-12-23 04:46:13
问题 I need to convert a text string contained in a variable to an XML node. The mapping to the output XML is determined by a file named 'mapping.xml'. It has bit manipulation(checking if bit is 1). mapping.xml <Root> <field no="2" charlength="2">variable</field> <field no="3" total="4">fixed</field> <field no="21" charlength="2"> <subfield no="1" total="3">fixed</subfield> <subfield no="2" charlength="2" idcode="ABC">variable</subfield> </field> <field no="63" charlength="2"> <format1> <subfield

What part of integer bit-representation is part of The Standard? [duplicate]

萝らか妹 提交于 2019-12-23 04:15:13
问题 This question already has answers here : What do the C and C++ standards say about bit-level integer representation and manipulation? (8 answers) What does the C++ standard state the size of int, long type to be? (24 answers) Closed 4 years ago . How much can one assume about integers' representation in memory? How portable way of slicing and splicing integers together are bit-wise operations? 回答1: The C standard discusses the representations of integer types in section 6.2.6.2. It specifies

Android, How to add a bitmap layer over another bitmap layer, dynamically from list?

社会主义新天地 提交于 2019-12-23 02:57:16
问题 In my application, I have a list adapter. each row includes an imageView in left and two textViews in right. For getting images I'm using Universal-Image-Loader library. my getView() method is this: @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = myInflater.inflate(R.layout.list_news_adapter, null); holder = new ViewHolder(); holder.ivIcon = (ImageView) convertView.findViewById(R.id.list_news_icon);

What is an intuitive way to interpret the bitwise operators and masking? Also, what is masking used for?

蹲街弑〆低调 提交于 2019-12-23 02:07:06
问题 I'm learning about bitwise operators and masking right now in my computer systems class. However I'm having some trouble internalizing them. I understand what the operators, &, |, ^, >> (both arithmetic and logical shift), and << DO, but I don't quite get what they're really used for aside from optimizing multiplication and division operations (for >> and <<), and to check if certain bits are on or off (the & operator). Also, I don't understand what masking is used for. I know that doing x &

Redistribute least significant bits from a 4-byte array to a nibble

[亡魂溺海] 提交于 2019-12-23 01:44:30
问题 I wish to move bits 0,8,16,24 of a 32-bit value to bits 0,1,2,3 respectively. All other bits in the input and output will be zero. Obviously I can do that like this: c = c>>21 + c>>14 + c>>7 + c; c &= 0xF; But is there a faster (fewer instructions) way? 回答1: c = (((c&BITS_0_8_16_24) * BITS_0_7_14_21) >> 21) & 0xF; Or wait for Intel Haswell processor, doing all this in exactly one instruction (pext). Update Taking into account clarified constraints and assuming 32-bit unsigned values , the

Bit shifting left

心已入冬 提交于 2019-12-23 01:35:12
问题 Let's say I want to bit shift i twice to the left and store the value in f . f = i << 2; Is that correct? How exactly do I do this in C/C++? 回答1: Yes. f = i << 2 Shifts are useful in a number of bit twiddling operations. This used to be a great way to multiply a number by four. However, these days, optimizing compilers tend to take care of that for you. Keep in mind that the two leftmost bits are discarded. 回答2: As an additional note: Even though your question is tagged C++ , it is probably

Bitwise equality

余生颓废 提交于 2019-12-22 11:34:02
问题 I need to perform a bitwise equality between two bytes. That means that for instance if I have two bytes: 00011011 and 00011110 the result is 11111010 The only fast way I see is to use the following statement byte a, b;//set input bytes byte c = ~(a^b);//output bytes But I wonder if there is a faster solution for this. After these equality operation I want to mask the bits I need. So I need to use an AND-operation. So the code becomes: byte a, b;//set input bytes byte m;//mask, intresting