Menu
  • HOME
  • TAGS

Why do we need a canonical format for the GUID?

random,guid,uuid,bits,canonicalization

It is so that if you update the algorithm you can change that number. Otherwise 2 different algorithms could produce the exact same UUID for different reasons, leading to a collision. It is a version identifier. For example, consider a contrived simplistic UUID format: 00000000-00000000 time - ip now suppose...

How to get nth bit values

c++,bit-shift,bits

Checking bits by numeric position is one of the correct ways to do so but it makes the code depend on magic numbers, which makes it harder to read and maintain. Usually, when checking for bitmasks, there is a goal of checking for some specific flag, let's say a hardware...

How many bits are necessary to encode an integer in the range of 0 to 31 (inclusive)?

encoding,bits

"Bits" are "binary digits". That means (by definition), they are digits in a base-2 number system. So instead of the base-10 system that you're used to (with digits 0-9 in each column), you only get two values (0 or 1) for each column. Every column in a base-10 system corresponds...

How to Convert Binary to Hex

binary,hex,byte,bits

Add it on the front, so that the result is 0101

Store 2 values in one variable

go,binary,bit-shift,bits

Is this Go code correct and portable It is correct, as long as you work with unsigned integers of 64-bit width. Portability in this case is provided by the sync/atomic packages to the architectures supported by the Go compiler. Note, however, that not all architectures support "true" atomic operations...

How is arctan implemented?

algorithm,floating-point,processor,bits

Trigonometric functions do have pretty ugly implementations that are hacky and do lots of bit fiddling. I think it will be pretty hard to find someone here that is able to explain an algorithm that is actually used. Here is an atan2 implementation: https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/ieee754/dbl-64/e_atan2.c;h=a287ca6656b210c77367eec3c46d72f18476d61d;hb=HEAD Edit: Actually I found this one:...

Fastest way to convert 7-bit values into bytes

c,embedded,byte,bits

You want to pack some bits from adjacent bytes into one byte. That can be achieved by combining the lowest 7 bits of the left byte shifted left with the lowest 7 bits of the right byte shifted right: void pack(const uint8_t in[16], uint8_t out[14]) { out[ 0] = (in[...

Bitwise Not for double in Java

java,bits

Java didn't supply the ~ operator on the floating-point types float and double because such an operation isn't meaningful. You can simulate it with this method: public static double performNotOnDouble(double d) { return Double.longBitsToDouble(~Double.doubleToLongBits(d)); } But the results don't have any real meaning: 0.0: NaN 1.0: -3.9999999999999996 10.0: -0.43749999999999994 -1.0:...

Python XOR error

python,python-3.x,xor,bits

As was already commented, chr() returns a string. In this question, you get advice about how to replace it. Similiarly, you should not .join() with '', the empty string, but with b'', the empty bytes object. Example: def bchr(i): return bytes([i]) def xor(s1,s2): return b''.join(bchr(i ^ j) for i, j...

How do I find the memory location of items on the stack? [closed]

memory,stack,bits

Let's look at some values and try to find a pattern. 0 -> 1000 1 -> 1008 2 -> 1016 It starts at 1000 and goes up by 8 each time, so n -> 1000 + 8*n 42 -> 1000 + 8*42 42 -> 1336 ...

Taking the bit of a file and displaying them

java,python,file,bits

Yes, it is possible to read bits from bytes while knowing the endianness of the host system. That is, if you want an exact representation of the file. import sys sys.byteorder open("fname").read(1) if (ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN)); // Java reads and writes bytes pretty naturally, also. You will need to use bitmasks to...

Masking a 16-bit value with 0xFFFF

javascript,masking,bits,16-bit

I think the important part is that you use JavaScript - it has only one numeric type - floating point. But apparently underlying engine can recognize when it should use integers instead - using bit mask is a strong suggestion that we want to use it as integer since bit...

How to store and write/load to/from file an arbitrary number of bits in Python?

python,bits

I think this does what you want. It uses the referenced bitio module to write/read the bits to/from a file. Operating systems generally require files to be a multiple of 8 bits in size, so this would end up creating a 24-bit (3 byte) file to store a single 20-bit...

How to compare a certain value to value in array?

java,arrays,for-loop,bits

If you want to output the values of the array, in my opinion the simplest method is probably a for-each loop: int[] skip = {3, 4, 8, 9, 12, 13, 16, 17, 20, 21}; for (int i : skip) { System.out.print(i + ","); } System.out.println(); If you wish to print...

CS_HREDRAW | CS_VREDRAW What does it do?

c++,c,windows,class,bits

The reason the flags can be OR'd is because they were purposefully written to not cancel each other out. If you look at the values of those flags you will see that: CS_HREDRAW = 0x0001 = 0000 0000 0000 0001 CS_VREDRAW = 0x0002 = 0000 0000 0000 0010 The result...

Why bits are numbered from right to left?

assembly,computer-science,computer-architecture,bits

Bits are not numbered from right to left. They are numbered from lowest weight (the lowest weight bit getting the number 0 or 1 depending on the convention chosen) to highest weight (which can be 15 or 16, 31 or 32, 63 or 64, ...). One reason to number them...

Binary Subtraction Borrowing Logic for 1000 - 0110?

binary,bits,subtraction

When you borrow, you carry over binary 10 to the lower bit, so: 0 ->1 0000 0110 ---- 0 Then you borrow again, and subtract 1 from 10, which is 1: 0 ->11 0000 0110 ---- 0 And finally: 0 ->11 0000 0110 ---- 0010 ...

High entropy random data creating functions?

math,random,bits,entropy

You are most likely looking for Pseudo-Random Number Generators. They are initialized by a seed, thus taking in a finite amount of entropy. Good generators have a decent entropy coming out, supposing you judge it only from its output (thus you ignore the seed and/or the algorithm to generate the...

How to output string of bits as a binary file in python

python,binary,bits

What you'd want is to split your string to 8-bit-chunks, and then convert them to bytes individually: for index in range(len(s)/8): substring = s[index * 8: index*8 + 8] byteval = int(substring, base=2) print chr(byteval) ...

checking bits in an NSData object

objective-c,c,byte,bits

This is what I ended up learning const char *byte = [fixtureStatusBasic bytes]; //objective c, puts the 2 bytes into *byte char n = byte[0]; //first byte is now called n if(n & 0b00111111){ //AND the byte "n" with 6 least significant bits set to 1 to see if any...

Bit manipulation (shifting)

c,bits

Certain compilers implement >> by rotating bits instead of shifting them. This guards against this case (if the last bit is set to 0, then even if the shifting is implemented as a rotation, the answer will remain correct)

Counting bits and printing result LC3 Assembly

assembly,output,bit-shift,bits,lc3

You're forgetting that OUT prints the character that the ASCII value points to. You would need to increase your answer by 48. You'll then run into problems when it comes to numbers of bits over 9, so you'd need to do some checking there. Hope that helps!...

Python Bits and bytes

python,bits

There is always the old fashioned trick of masking: >>> bits = bin(byte[0] & 0x03) >>> bits '0b10' ...

Return list of active bits [closed]

python,bits

Alternative dirty one liner (no strings attached): input_number = 220 print [i for i in xrange(input_number.bit_length()) if ((1 << i) & input_number)] ...

Converting the bits of a vector to a decimal integer

c++,vector,decimal,bits

I think you meant: for ( unsigned int i = 0; i < keyReg.size(); i++) { if (keyReg[i]==1) { key = key+(1 << i); // this is 2^i cout << key << "\n"; } } ^ is a bitwise operator for XOR so the code was "valid" from the compiler's...

Writing a bit flip algorithm

python,algorithm,bits

Your output is correct. The 32-bit unsigned complement of 3 is indeed 4294967292. The full output produced by your program is: 4294967292 2147483648 4294967294 4294967295 which corresponds correctly to the numbers 3, 2147483647, 1, 0. You can more easily see this if you write them in hex: Dec Hex ~Hex...

String of bits to Unicode

c#,string,unicode,bits

There's a couple of problems with your code. First BitArray will reverse the bit order - it's easier to use Convert.ToByte Your input string contains two bytes (one per character), but you're using Encoding.Unicode to decode it, which is UTF16 encoding (two bytes per character), you need to use Encoding.UTF8...

Directly manipulating bits in assembly for bit stuffing

assembly,32-bit,bits,insertion,bitstuffing

Among two popular instruction set architectures I've worked with (x86 and MIPS), there are no machine instructions to insert bits into the middle of an integer like you described. However, the functionality can be implemented in terms of bitwise shifts, AND, OR, NOT, which are available on all processors and...

Insert 1 bit in number c#

c#,insert,numbers,bits

You can use System.Collections.BitArray for this task, it allows you to convert a number to a bit array, then manipulate individual bits of this number, then convert it back. Here's how you could insert a bit into a number at a certain position: public static byte insertIntoPosition(byte number, int position,...

Setting groups of bits from a structure to a value

c,struct,bits

#include <assert.h> #include <stdlib.h> typedef struct { char x1 : 1; char x2 : 1; char x3 : 1; char x4 : 1; char x5 : 1; char x6 : 1; char x7 : 1; char x8 : 1; unsigned int an_int; } my_struct; int main(int argc, const char* argv[])...

Need help to work with characters longer than 2 or more bytes in Python

python,binary,byte,bits

It's not that hard. Of course there's a system to it - else no program could print or edit names like Ñáñez... The upper bits in each byte indicate what is the status of that byte: 1) if bit 7 is 0, then it's just ASCII (*0*1110111 = w) 2)...

segmentation fault while trying to manipulate bits

c,pointers,bit-shift,bits

You're casting a non-pointer value to a pointer: (byte_pointer) x and then dereferencing it, this won't work. Try to take the address of x and cast that: (byte_pointer) &x instead. Make this change and your program will run, but be aware that casting an int to an unsigned char might...