Add a bit value to a string

how to insert bit value in sql server
add two binary strings
sql bit true/false
sql server bit data type example
how to add binary strings in c++
what is bit string in discrete mathematics
add binary strings interviewbit
add two binary strings python

I am trying to send a packet over a network and so want it to be as small as possible (in terms of size).

Each of the input can contain a common prefix substring, like ABCD. In such cases, I just wanna send a single bit say, 1 denoting that the current string has the same prefix ABCD and append it to the remaining string. So, if the string was ABCDEF, I will send 1EF; if it was LKMPS, I wish to send the string LKMPS as is.

Could someone please point out how I could add a bit to a string?

Edit: I get that adding a 1 to a string does not mean that this 1 is a bit - it is just a character that I added to the string. And that exactly is my question - for each string, how do I send a bit denoting that the prefix matches? And then send the remaining part of the string that is different?


In common networking hardware, you won't be able to send individual bits. And most architectures cannot address individual bits, either.

However, you can still minimize the size as you want by using one of the bits that you may not be using. For instance, if your strings contain only 7-bit ASCII characters, you could use the highest bit to encode the information you want in the first byte of the string.

For example, if the first byte is:

0b01000001 == 0x41 == 'A' 

Then set the highest bit using |:

(0b01000001 | 0x80) == 0b11000001 == 0xC1

To test for the bit, use &:

(0b01000001 & 0x80) == 0
(0b11000001 & 0x80) != 0

To remove the bit (in the case where it was set) to get back the original first byte:

(0b11000001 & 0x7F) == 0b01000001 == 0x41 == 'A'

Add two bit strings, //adds the two-bit strings and return the result. string addBitStrings( string first Returns the the new length. // Using StringBuilder as Java only uses call by value​. Length of the substring to be copied (if the string is shorter, as many characters as possible are copied). A value of string::npos indicates all characters until the end of str. s. Pointer to an array of characters (such as a c-string).


If you're working with a buffer for use in your communications protocol, it should generally not be an std::string. Standard strings are not intended for use as buffers; and they can't generally be prepended in-place with anything.

It's possible that you may be better served by an std::vector<std::byte>; or by a (compile-time-fixed-size) std::array. Or, again, a class of your own making. That is especially true if you want your "in-place" prepending of bits or characters to not merely keep the same span of memory for your buffer, but to actually not move any of the existing data. For that, you'd need twice the maximum length of the buffer, and start it at the middle, so you can either append or prepend data without shifting anything - while maintaining bit-resolution "pointers" to the effective start and end of the full part of the buffer. This is would be readily achievable with, yes you guessed it, your own custom buffer class.

Bit string data types - Documentation, INSERT statements must enclose binary values in single‑quote marks, preceded by b for a bit string and x for a hexadecimal string. The following table lists the  The java.lang.Byte.valueOf(String s) returns a Byte object holding the value given by the specified String. The argument is interpreted as representing a signed decimal byte, exactly as if the argument were given to the parseByte(java.lang.String) method. The result is a Byte object that represents the byte value specified by the string.


I think the smallest amount of memory you can work with is 8 bits.

If you wanted to operate with bits, you could specify 8 prefixes as follows:

#include <iostream>

using namespace std;

enum message_header {
    prefix_on = 1 << 0,
    bitdata_1 = 1 << 1,
    bitdata_2 = 1 << 2,
    bitdata_3 = 1 << 3,
    bitdata_4 = 1 << 4,
    bitdata_5 = 1 << 5,
    bitdata_6 = 1 << 6,
    bitdata_7 = 1 << 7,
};

int main() {
    uint8_t a(0);

    a ^= prefix_1;

    if(a & prefix_on) {
        std::cout << "prefix_on" << std::endl;
    }
}

That being said, networks pretty fast nowadays so I wouldn't do it.

MySQL 5.7 Reference Manual :: 9.1.5 Bit-Value Literals, By default, a bit-value literal is a binary string: mysql> SELECT b'1000001', Ways to do this include adding 0 or using CAST( AS UNSIGNED) . For example​  Here: The program creates a bool array with true and false values, and then the BitArray constructor converts those into one bit each. Bool Array. Info: This means that instead of one byte for a bool, the values are stored as one bit, in one-eighth the space. True, False


An Essential Guide To SQL Server BIT Data Type, If you insert a string value of True into the bit column, SQL server converts it to bit 1: INSERT INTO test.sql_server_bit (bit_col) OUTPUT inserted.bit_col VALUES  Appends the string representation of a specified object to this instance. Appends the string representation of a specified string builder to this instance. Appends the string representation of a specified 16-bit unsigned integer to this instance. Appends the string representation of a specified 32-bit unsigned integer to this instance.


Documentation: 7.3: Bit String Types, Note: If one explicitly casts a bit-string value to BIT(n), it will be truncated or ERROR: Bit string length 2 does not match type BIT(3) INSERT INTO test VALUES  Public Function Add(ByVal value As Object) As Integer Implements IList.Add If _count < _contents.Length Then _contents(_count) = value _count += 1 Return _count - 1 End If Return -1 End Function Public Sub Clear() Implements IList.Clear _count = 0 End Sub Public Function Contains(ByVal value As Object) As Boolean Implements IList.Contains For i As Integer = 0 To Count - 1 If _contents(i) = value Then Return True Next Return False End Function Public Function IndexOf(ByVal value As Object) As


bit (Transact-SQL), An integer data type that can take a value of 1, 0, or NULL. The string values TRUE and FALSE can be converted to bit values: TRUE is  Add two bit strings Given two bit sequences as strings, write a function to return the addition of the two sequences. Bit strings can be of different lengths also. For example, if string 1 is “1100011” and second string 2 is “10”, then the function should return “1100101”.