bit-manipulation

When using cout and cin, what are the “<<” and “>>” operators doing and why do we use them?

一笑奈何 提交于 2021-02-05 07:35:21
问题 For example: int age; cin >> age; cout << "You are " << age << " years old!" << endl; Why do we use the "<<" and ">>" operators here? What are they doing? I somewhat understand bit-shifting, but I don't get how that works here. 回答1: They are called the stream insertion operator ( << ) and the stream extraction operator ( >> ). These are the same operators as the left and right bit shift operators (even though they have different names). The bit shift operators are overloaded, so that when the

can't shift negative numbers to the right in c

不打扰是莪最后的温柔 提交于 2021-02-05 06:25:06
问题 I am going through 'The C language by K&R'. Right now I am doing the bitwise section. I am having a hard time in understanding the following code. int mask = ~0 >> n; I was playing on using this to mask n left side of another binary like this. 0000 1111 1010 0101 // random number My problem is that when I print var mask it still negative -1. Assuming n is 4. I thought shifting ~0 which is -1 will be 15 (0000 1111). thanks for the answers 回答1: Performing a right shift on a negative value

Do .net FlagsAttribute enums need to be manually valued?

故事扮演 提交于 2021-02-05 05:49:11
问题 To allow for different formatting options on a method that displays a news story, I've created an enumeration that can be passed in to specify how it gets displayed. [Flags] private enum NewsStyle { Thumbnail = 0, Date = 1, Text = 2, Link = 4, All = 8 } string FormatNews( DataRow news, NewsStyle style ) { StringBuilder HTML = new StringBuilder(); // Should the link be shown if ( ((newsStyle & NewsStyle.All) == NewsStyle.All || (newsStyle & NewsStyle.Link) == NewsStyle.Link)) { HTML

Python proper use of __str__ and __repr__

核能气质少年 提交于 2021-02-04 17:14:22
问题 My current project requires extensive use of bit fields. I found a simple, functional recipe for bit a field class but it was lacking a few features I needed, so I decided to extend it. I've just got to implementing __str__ and __repr__ and I want to make sure I'm following convention. __str__ is supposed to be informal and concice, so I've made it return the bit field's decimal value (i.e. str(bit field 11) would be "3" . __repr__ is supposed to be a official representation of the object, so

Swapping pair of bits in a Byte

强颜欢笑 提交于 2021-02-04 10:16:34
问题 I have an arbitrary 8-bit binary number e.g., 11101101 I have to swap all the pair of bits like: Before swapping: 11-10-11-01 After swapping: 11-01-11-10 I was asked this in an interview ! 回答1: In pseudo-code: x = ((x & 0b10101010) >> 1) | ((x & 0b01010101) << 1) It works by handling the low bits and high bits of each bit-pair separately and then combining the result: The expression x & 0b10101010 extracts the high bit from each pair, and then >> 1 shifts it to the low bit position. Similarly

Swapping pair of bits in a Byte

北慕城南 提交于 2021-02-04 10:12:21
问题 I have an arbitrary 8-bit binary number e.g., 11101101 I have to swap all the pair of bits like: Before swapping: 11-10-11-01 After swapping: 11-01-11-10 I was asked this in an interview ! 回答1: In pseudo-code: x = ((x & 0b10101010) >> 1) | ((x & 0b01010101) << 1) It works by handling the low bits and high bits of each bit-pair separately and then combining the result: The expression x & 0b10101010 extracts the high bit from each pair, and then >> 1 shifts it to the low bit position. Similarly

Swapping pair of bits in a Byte

有些话、适合烂在心里 提交于 2021-02-04 10:09:53
问题 I have an arbitrary 8-bit binary number e.g., 11101101 I have to swap all the pair of bits like: Before swapping: 11-10-11-01 After swapping: 11-01-11-10 I was asked this in an interview ! 回答1: In pseudo-code: x = ((x & 0b10101010) >> 1) | ((x & 0b01010101) << 1) It works by handling the low bits and high bits of each bit-pair separately and then combining the result: The expression x & 0b10101010 extracts the high bit from each pair, and then >> 1 shifts it to the low bit position. Similarly

x86 XOR opcode differences

橙三吉。 提交于 2021-02-04 07:24:11
问题 looking at http://ref.x86asm.net/coder32.html I found two opcodes that match for the statement xor eax,eax 1) opcode 31 XOR r/m16/32 r16/32 2) opcode 33 XOR r16/32 r/m16/32 both refers to 32bit register for operand1 and operand2. So, is there any differences in this specific case of the XORing two 32bit registers ? 回答1: x86 has 2 redundant ways to encode a 2-register instance of any of the basic ALU instructions that have r/m source and r/m destination forms. This redundancy is a consequence

different results between javascript and python when using bitwise operators

廉价感情. 提交于 2021-01-29 16:59:49
问题 I am trying to translate some js code into python and having trouble converting bitwise operators. I already introduced ctypes.c_int in python but the results still do not match. For the >>> in js I used as suggested here. A minimal example of my (not working) code: Javascript: let R = [7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6,

Performing modulo 2 addition in MIPS, 32 bit integers

≯℡__Kan透↙ 提交于 2021-01-28 18:20:35
问题 Followup to this I figured out what went wrong in the linked post. The Sigma0 and Sigma1 boxes weren't doing what they should: modulo 2 addition of the rotated versions of either A or E. Basically, it should work like this: Notice that if the number of bits in a position is even the result is 0, if odd it's 1. To do this I made a truth table and karnaugh maps: LINK I simplified the resulting expression to !A!BC + A(B XNOR C) And I tried to apply it to Sigma0 and Sigma1. But it's not working.