FAQ Database Discussion Community


Last two bits in a BitArray of length 4 are being ignored when converting to int

c#,binary,int,bit,bitarray
Consider the following BitArray: BitArray bitArray = new BitArray(new Boolean[] {true, true, false, false}); and in binary equals: 1100 Now I want to convert this to an int and have tried to use the methods described on this page: How i can convert BitArray to single int? However, both these...

golang: bitwise opration on very long binary bit string representation

string,go,bit,bitwise-operators
As an exercice, in input I got 2 very big string containing long binary representation here a short one but could have more than 100 bits: Example 11100 00011 With output in bitwise OR (as string) 11111 My approach was to parse each string characters and make a bitwise OR...

How to get All bit field value in SQL Server in stored procedure

sql-server,stored-procedures,where,bit
I have been searching but still not get the best way to do this I have a table with sex column (bit) so the value will be 1/0/null My stored procedure with parameter @sex SELECT * FROM [dbo].[data] WHERE sex = ISNULL(@sex, sex) When I'm send @sex null, the query...

Reducing image bit depth

matlab,image-processing,bit
I'm working on 800 x 800 pixels images with a bit depth of 24 (PNG format). I assume it means 3 x 8 bits. These images are just black and white (0 or 255). I want to reduce this depth to 8 bit because*, when I process this images in...

Unpack redis set bit string in Go

go,redis,bit-manipulation,bit
Using redis#Setbit to set bit in a key like: redis.Do("SETBIT", "mykey", 1, 1). When I read it using redis#Get like redis.Do("GET", "mykey"), I get a bit string. How do I unpack the string so I can get a slice of bools in Go? In Ruby, you use String#unpack like "@".unpack...

Jump to Address after Branching in For Loop in MIPS

c,byte,branch,mips,bit
I am trying to code a program that checks if the 16 bits in an integer is a one or zero. I chose to implement this by shifting right one bit 15 times and checking if the first bit in each shift is a zero or non zero. Then, if...

Is 2s complement a way to store negative number?

c++,c,binary,bit
I have read many articles and SO answers to understand 2s complement. They have helped me a lot. However, there are few doubts in my mind about 2s complement. 1) Is 2s complement a way to store negative numbers in order to make operation easy or have some other applications...

How to print all set bits of a negative integer

java,bit
while (n != 0) { System.out.print(n & 1); n = n >> 1; } The above code in Java leads to an infinite loop, if n = -1. How do I get it to print negative integers?...

How to shift 111 starting with 00000001 Assembler

assembly,byte,bit,avr,shift
I need to shift value of register in a loop. I need to start with a 00000001 -> 00000011 -> 00000111 -> 00001110 -> 00011100 -> 00111000 -> 01110000 -> 11100000 -> 01110000 -> 00111000-> 00011100-> 00001110-> 00000111 -> 00000011 -> 00000001 . I have no idea how to do...

How can you get the j first most significant bits of an integer in C++?

c++,bit
I know that to get the first j least significant bits of an integer you can do the following: int res = (myInteger & ((1<<j)-1)) Can you do something similar for the most significant bits?...

Theoretically, is comparison between 0 and 255 faster than 0 and 1?

performance,comparison,bit,low-level,machine-code
From the point of view of very low level programming, how is performed the comparison between two numbers? Using one byte, unsigned numbers 0, 1 and 255 are written: 0 -----> 00000000 1 -----> 00000001 255 ---> 11111111 Now, what happens during the comparison between these numbers? Using my vision...

Java Integer to Binary confusion

java,bit
According to my comprehension, Integer type in Java is 32-bit-signed, the most significant bit is the signed bit. This is why Integer.MAX_VALUE is 2147483647, which is: 1111111111111111111111111111111(1 repeated in 31 times). So I assume that it actually can be represented as: 01111111111111111111111111111111(a 0 followed by 1 repeated 31 times) The...

How to convert bit string into integer?

c,bit
Is there any way to convert a 2'scomplement bit string into a integer? I think it's easy enough for positive numbers, but as for negative numbers I'm a little confused. #include <stdio.h> int bits2Integer(char bitString[]){ int value = 1; int power = 1; int constantIncrement = 2; if(bitString[0] == 0)...

bit change location in bit stream

php,arrays,bit
bit stream: 0 0 0 1 1 1 1 0 0 1 1 0 0 0 1 0 desired outbpu: 4-7,10-11,15 my function: $flag = 0; $str = ''; for( $i=0; $i < count( $array ); $i++ ){ if( 0 == $array[$i] ){ $flag = 0; continue; } if( $i...

Flip all bit values in a byte [duplicate]

java,byte,bit
This question already has an answer here: Bitwise operator for simply flipping all bits in an integer? 7 answers I would like to flip all the bits in a byte. How would I do that? Ex: input: 10101000 output: 01010111 ...

LC3 Bit Counter

bit,lc3,bitcount
I'm trying to figure out how to implement a bit counter in LC3 assembly language. ex: input "00001100001000001" output "000000000000100" I would be counting the number of ones in the string of bits and outputting that number in binary. I know how to do this given one bit at a...

SQL Server 2012 How to change the data type of a column from bit to datefield?

datetime,sql-server-2012,bit,data-type-conversion,sqldatatypes
I have a table Person with a column called onvacation. This column is of data type bit since it's a boolean in the code. It has values null, 0 and 1. I would like to change the data type of this column from bit to datetime so that all values...

Bitwise operations and shifts problems

c++,c,bit
I am testing the function fitsBits(int x, int n) on my own and I figure out there is a condition that doesn't fit in this function, what is the problem? /* * fitsBits - return 1 if x can be represented as an * n-bit, two's complement integer. * 1...

Confused about how to perform bitwise operations on 16bit binary number in python shell

python,shell,bit,bitwise-operators
I'm learning about bitwise operators in python and so I tried to do some experimentation in the shell. I tried doing bit manipulation with 8bit numbers and that seems to work normally, but if I try to use 16bit numbers this happens: >>> b = 0010000000000000 >>> b 549755813888 If...

Checking byte with bitwise operators

c,bit-manipulation,byte,bit
I could use this: unsigned long alpha = 140 | 130 << 8 | 255 << 16; to set 140 to the first byte of alpha, 130 to the second and 255 as 3rd. How do I do the opposite (i.e checking a specific byte of alpha) ? alpha &...

How to determine if more than one bit in an STD_LOGIC_VECTOR is set in VHDL

vhdl,bit
I am wondering how I can determine if more than one bit of a four-bit STD_LOGIC_VECTOR is set to '1'. e.g if it is "1001" or "1100" or "1111". I am writing a program where I have to set an error signal to '1' if I get more than one...

C++: digits vs bits?

c++,boost,standards,bit,digits
I am trying to understand the difference of vocabulary used in the C++ language between digits and bits as in : CHAR_BIT; std::numeric_limits<char>::digits; Is there a conceptual difference? Maybe for weird architectures? If so, what would be called the result of the operator[] of std::bitset. Does it give access to...

Java String to bit array

java,arrays,string,bit
Considering the following String String hexData = "1E01"; Is there a simple implementation to turn any hexData into a bit-based String array, like String hexDataBits = "0001111000000001"; ?...

invert n different random bits in int

c++,math,random,bit
How could i efficiently invert n bits in unsigned int x? Each bit must be picked randomly. Also it must be picked only once. Hamming distance between x and returned result must equals n. Is there an O(n) solution? // rnd_engine is kind of std::minstd_rand template< typename rnd_engine > unsigned...

Recursive bits reverse program

c,recursion,reverse,bit
Recursive reverse bits program. #include <stdio.h> #include <conio.h> void displayBits( unsigned value ); void reverseBits( unsigned value, unsigned c ); int main( void ) { unsigned value; unsigned c = 32; printf( "Enter a unsigned integer: " ); scanf( "%u", &value ); displayBits( value ); printf( "%10s", "Reversed: " );...

How does this reverseBytes method work?

c,byte,bit,bit-shift
I was looking at this function online and am wondering how it works: /* * reverseBytes - reverse bytes * Example: reverseBytes(0x12345678) = 0x78563412 * Legal ops: ! ~ & ^ | + << >> */ int reverseBytes(int x) { int newbyte0 = (x >> 24) & 0xff; int newbyte1...

Rational comparison of bits

c++11,int,bit-manipulation,bit
I have a number of type int. It lies within [0,255]. That is, includes 8 bits. I need to check often say: 2(int) = 00000010(Binary) 1. The bit 6 and bit 7 must be equal to 0 and 1 respectively. And I check it like this: if ((!(informationOctet_ & (1...

using bitwise and operator c++

c++,bit-manipulation,bit,bitwise-operators
I have the following code int n = 50; while(n) { //1 if(n & 1) cout << "1" << endl; //2 //right shift the number so n will become 0 eventually and the loop will terminate n >>= 1; //3 } When we use bitwise and 1 (& 1) with...

Solving bits equations

c++,algorithm,pascal,bit
I have the bit equation: x + y = x | y How to solve this equation? I need to find k-th smallest positive integer y for which equation holds. Maybe there is any algorithm? Where can i read about it? Ofcause i simply tried to solve it like this...

How to present letter as a bitmap?

objective-c,bit,letters
I know how to present number as a bitmap, for instance: 17 = 010001 11 = 001011 This is about numbers, but what about letters? Is there a way to do this? For example: w = ?? [ = ??...

Check if bit is changed

c,bit
I want transmit the state of a bit. When it is set, it should be transmetted for 10 secondes even if its status changes. here is my solution: unsigned long Time; unsigned char State; unsigned char Flag;/*It is set by an other function*/ unsigned char Bit; #define BITDETECTION 1 #define...

Bit Pattern in JavaScript [duplicate]

javascript,bit
This question already has an answer here: String Conversion in Javascript (Decimal to Binary) 5 answers I would like to ask help whether am I doing the right thing or not. You see I am trying to test myself by displaying the bit pattern of a number in the...

Having trouble understanding a portion of code

c,algorithm,bit
I have this problem: You have n problems. You have estimated the difficulty of the i-th one as integer ci. Now you want to prepare a problemset for a contest, using some of the problems you've made. A problemset for the contest must consist of at least two problems. You...

What happened to the byte 0xFF (11111111) in Java?

java,sockets,networking,bit,bitwise-operators
I need to send the byte 0xFF to a client, but when I do this: byte toSend = 0b11111111; // or byte toSend = 0xFF; I get a compile error sending it can't convert from int to byte. I want to make sure I am sending a byte with all...

bit comparison in loop on AVRs

c,bit,avr,led,operations
I'm learning about bit logic in C on AVRs and I have a problem. I want to compare an "i" bit (from the right) from int8_t variable and if it is 1, then do the next instruction, but it doesn't work. Here's what I write: if (variable & (1<<i)==(1<<i)) instruction;...

How to bit-shift and concatenate to get correct result?

c#,concatenation,bit,bit-shift,uint16
I'm currently struggling with modbus tcp and ran into a problem with interpreting the response of a module. The response contains two values that are encoded in the bits of an array of three UInt16 values, where the first 8 bits of r[0] have to be ignored. Let's say the...

Is there a fast way/trick to add one bit at the beginning of a file?

c++,optimization,bit,bitwise-operators
For a special algorithm I have to add (or remove) several times one bit at the beginning of a file. It must be a bit and not a whole byte like '0000 0001'. After that I don't have to overwrite the file with the new content, so it is sufficient...

Extracting 3 bytes to a number

c,bit,bitwise-operators
What is the FASTEST way, using bit operators to return the number, represented with 3 different unsigned char variables ? unsigned char byte1 = 200; unsigned char byte2 = 40; unsigned char byte3 = 33; unsigned long number = byte1 + byte2 * 256 + byte3 * 256 * 256;...

troubles with understanding how ASCII works

encoding,byte,ascii,bit
I have few questions about ASCII encoding: How come there is 127 characters definable in ASCII coding? It should be 7-bit, which means 2 to the power of 7 which equals 128. Where is the one character missing? When I save a textfile as a textfile.txt it should be saved...

Function to XOR two 128 bits. How do I generate 128 bit values?

c#,byte,bit,xor,bitarray
I'm trying to learn simple cryptology and as a starter I'm trying to achieve the following. A function, taking two 128 bit params (key and plaintext) as input and returning their XOR. I know XOR is not safe but I'm starting out with a simple example. This is what I...

Swift Bit array to Bytes array (UInt8 array)

arrays,swift,type-conversion,byte,bit
I have array with bits: var bits: [Bit] and how could I convert it to bytes array: var bytes: [UInt8] For example I have 280 bits and I should have 35 UInt8 in bytes array. I can think of solution where I take 8bits and check if first is true,...

How to obtain bits from a file in matlab? [duplicate]

matlab,bit
This question already has an answer here: int array to binary array 2 answers I need to encode bits from a file in a signal. Does anyone know how to get the bit representation in a matlab variable? I'd need a vector with each bit of the data. Using...

Square root union and Bit Shift

c,union,bit,shift
I found this code with which the square root is obtained what surprises me is the way it does, using a union and bit shifts this is the code: float sqrt3(const float x) { union { int i; float x; } u; u.x = x; u.i = (1<<29) + (u.i...

Setting a bit in hexadecimal number

c,bit-manipulation,bit
given a number (int a = 0XABCDE98) i am trying to set the D bit to 6. ie after the Bit manipulation the number should be (0XABC6E98). I have written the small C program to do the bit manipulation, but somehow i am not able to see the correct bit...

Numpy: check whether a bit is set to 1 or 0 in an array?

python,numpy,bit-manipulation,bit,bitstring
Suppose the following: bitstring = numpy.random.random_integers(0,2**32,size=8).astype(numpy.uint32) How can I find out which of the 256 bits are set to 1? I've got this... but this is crazy, isn't it? maximum = (2**32)-1 for checkbit in range (256): yes = bool(numpy.bitwise_and((2**checkbit)%maximum, bitstring[ ( (checkbit // maximum) + checkbit % maximum )...

Back and forth calculation of integer Identifier using bit shifting and OR operations

c#,.net,bit-manipulation,bit
Good morning, don't know if this question is hitting the right StackExchange network. If not please forgive me. I got an decimal/integer identifier value which i need to split into three separate values. Lets assume we have program ABC. Program ABC is generating the decimal/integer identifier value using this formula:...

C Write Bit in Memory

c,memory,binary,bit
If I have an 8 bit number like so, 00101001, how would I replace one of the bits with a 1 (setting the bit)? how would I replace one of the bits with a 0? Currently in my code I have two functions as shown: void ADDSET(unsigned char *signalmask, int...

Rotate right using bit operation in c

c,bit-manipulation,bit
I am trying to come up with a function int rotateRight(int x, int n) that Rotate x to the right by n. For example,rotateRight(0x87654321,4) = 0x76543218. This is what I have so far int rotateRight(int x, int n) { int mask = (((1 << n)-1)<<(32-n)); int reserve = (int)((unsigned) (x&mask)...

How to generate a new int with a specific amount of bits in it

java,bit-manipulation,bit
The Problem Hi there, i recently moved to using a binary representation for a domain in my program, this enhanced the speed of calculations with that domain, but i now need a method of generating ints based on an input number of bits needed. My application is in Java so...

How to get specific bit segments of an integer in C?

c,bit-manipulation,bit
You are given a getTemps() function returns an integer composed of: the daily high temperature in bits 20-29, the daily low temperature in bits 10-19, and the current temperature in bits 0-9, all as 2's complement 10-bit integers. Write a C program which extracts the high, low, and current temperature...

MySQL TINYINT(1) versus BIT(1)

mysql,bit,tinyint
Please enlighten me... Which data type consumes the smallest, TINYINT(1) or BIT(1)? I know that TINYINT(1) and BIT(1) is considered the same according to majority of answers here. Doesn't TINYINT(1) accepts 0-9 while BIT(1) can only 1 or 0. From the looks of it TINYINT uses larger storage because it...

How to do a bit representation in a C-standard way?

c,bit-manipulation,standards,bit
As per the C standard the value representation of a integer type is implementation defined. So 5 might not be represented as 00000000000000000000000000000101 or -1 as 11111111111111111111111111111111 as we usually assume in a 32-bit 2's complement. So even though the operators ~, << and >> are well defined, the bit...

What is bits in computer science

64bit,bit
I watched a video on youtube about the bits. After watching it I have a confusion what the actual size does a number, string or any character takes. What I have understood from the video is. 1= 00000001 // 1 bit 3= 00000011 // 2 bits 511 = 111111111 //...

How to efficiently check against bitmask?

c++,c,bit,bitmask
I am using inotify and want to efficiently check against the reported bitmask event (see inotify man page). Now I could brutely check against every bit on every event, but that would be extremely crude, if not stupid, as I would have N conditionals every time. Or is calling (...

A 32 bit register represents 4Gbyte or 4Gbit of memory?

byte,bit
It may be a very stupid question but everywhere I read, it says a 32 bit register can represent maximum of 4GByte of memory but should it represent 4Gbit of memory? As 2^2 . 2^30 gives 4G. For addition of byte there must be another factor of 2^3. Can anyone...