bits

printing the binary representation of a number

陌路散爱 提交于 2019-12-10 06:39:29
问题 What is wrong with the below code which prints the binary representation of a number? int a = 65; for (int i = 0; i < 8; i++) { cout << ((a >> i) & 1); } 回答1: You're starting at the least significant bit in the number and printing it first. However, whatever you print first is the most significant digit in the typical binary representation. 65 is 01000001 so this is how your loop iterates 01000001 ^ Output: 1 01000001 ^ Output: 10 01000001 ^ Output: 100 ... 01000001 ^ Output: 10000010 Thus

bitParity - Finding odd number of bits in an integer

Deadly 提交于 2019-12-09 07:11:31
问题 I have to create a function bitParity(int x) that takes an integer and returns 1 if there is an odd number of 0 's in the bit form of x , and 0 otherwise. Ex: bitParity(5) = 0, bitParity(7) = 1 However, this is difficult as I can only use bit operators on this problem ( ! ˜ & ˆ | + << >> are the only legal ones). That means, no loops, if-then , or anything of the sort. Constants can be used. So far, what I have doesn't work, but I figured that I should shift the bits of the integer 16 , 8 ,

What is the fastest way to calculate the number of bits needed to store a number

谁说我不能喝 提交于 2019-12-09 02:59:30
问题 I'm trying to optimize some bit packing and unpacking routines. In order to do the packing I need to calculate the number of bits needed to store integer values. Here is the current code. if (n == -1) return 32; if (n == 0) return 1; int r = 0; while (n) { ++r; n >>= 1; } return r; 回答1: You're looking to determine the integer log base 2 of a number (the l=highest bit set). Sean Anderson's "Bit Twiddling Hacks" page has several methods ranging from the obvious counting bits in a loop to

Double.doubleToLongBits equivalent in C#?

筅森魡賤 提交于 2019-12-08 19:17:59
问题 there's a Java method Double.doubleToLongBits that basically gets a double and return a long with the same bits. How can I do it in C#? Thank you 回答1: BitConverter.DoubleToInt64Bits would be a good alternative. http://msdn.microsoft.com/en-us/library/system.bitconverter.doubletoint64bits.aspx 回答2: You'll want BitConverter.DoubleToInt64Bits 来源: https://stackoverflow.com/questions/6816691/double-doubletolongbits-equivalent-in-c

How are fractions represented in computers?

泪湿孤枕 提交于 2019-12-08 16:29:48
问题 Since computers think in terms of "1" and "0" how do they calculate and represent fractions such as 7.50 ? I know Java and JavaScript and if required for the answer you can use them as an example . Edit : I was watching this MIT video on hashing by Prof. Cormen at 46:31 seconds he explains the multiplication hash function using a modular wheel which is a unit circle with several points in it and the points denote fractions. This prompted me to ask this basic question here in SO . 回答1: The

CRC 16-CCITT with lookup table

风格不统一 提交于 2019-12-08 11:35:27
问题 So what I know about CRC, and also did a Java implementation is this: Having an initial message as a 16-bit polynomial, for instance 0x0617 65 0000.0110.0001.0111 this one gets another 16 of 0 bits 0000.0110.0001.0111|0000.0000.0000.0000 Then, having the divisor, 0x1021 0001.0000.0010.0001 (0, 5, 12) We allign it at the start of each "1" in our initial message, and do XOR between the bits, until there are no more 1s in the initial message. In total, there will be 6 XORs in our example. The

Converting bytes to Int64s/Floats/Doubles in Haskell

旧时模样 提交于 2019-12-07 08:58:53
问题 I'm trying to parse a binary file format in Haskell (Apple's binary property list format), and one of the things required by the format is to treat sequences of bytes as either (a) unsigned 1-, 2-, or 4-byte integers; (b) signed 8-byte integers; (c) 32-bit float s; and (d) 64-bit double s. Converting sequences of bytes to unsigned integers is easy, and even dealing with signed integers wouldn't be terrible . But for signed integers, and especially for Float s and Double s, I don't really want

How to convert string into bits and then into int array - java

冷暖自知 提交于 2019-12-07 05:39:50
问题 How to convert string into bits(not bytes) or array of bits in Java(i will do some operations later) and how to convert into array of ints(every 32 bits turn into the int and then put it into the array? I have never done this kind of conversion in Java. String->array of bits->(some operations I'll handle them)->array of ints 回答1: ByteBuffer bytes = ByteBuffer.wrap(string.getBytes(charset)); // you must specify a charset IntBuffer ints = bytes.asIntBuffer(); int numInts = ints.remaining(); int

Most efficient way to find the index of the only '1' bit in a char variable (in C)

亡梦爱人 提交于 2019-12-07 05:13:13
问题 This is an interview question: You are given a char variable named ch , when you know that it represents a number that in its binary form, only one of its eight bits will be equal to '1'. I.E. , the only possible values for ch are: 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80 . Given the variable ch , I need to write the most efficient code to get the index of that '1' bit. For example: if ch == 0x1 -> result is 0. if ch == 0x4 -> result is 2. The obvious way is to use switch-case, but I need

IP address input in C

一笑奈何 提交于 2019-12-06 06:17:18
I want to write a program in C which is taking input IP Address from user and then i want to do some bit operations on it. How can i take input in bits in C. I tried the below code but integer is of size 2 bytes, which makes the complete address here of 8 bytes(64 bits). When using char to scan input, its losing the value entered. Is there any way to take input in bits( i want 32 bits IPv4 address in 32 bits only and 128bit V6 in 128 bits only). unsigned short int a,b,c,d; scanf("%d.%d.%d.%d", &a,&b,&c,&d); printf("%d\t%d\t%d\t%d\t", a, b, c, d); Thanks in advance. #include <stdio.h> int main