for example
> SETBIT bitmapsarestrings 2 1
> SETBIT bitmapsarestrings 3 1
> SETBIT bitmapsarestrings 5 1
> SETBIT bitmapsarestrings 10 1
> SETBIT bitmapsarestrings 11 1
> SETBIT bitmapsarestrings 14 1
> GET bitmapsarestrings
"42"
Binary storage should not like this: 0010 0110 0001 1100 ?
stored in this way, why value is 42?
These SETBIT operations will make the value as a binary string, whose length is 2 bytes or 16 bits. After the settings, the value will be 0b 00110100 00110010 in binary format.
The first byte (0b 00110100) is 52, which is the ascii code of '4', and the second byte (0b 00110010) is 50, which is the ascii code of '2'. So when you get the value of the string, it returns "42".
What #for_stack said, or just refer to the lines immediately above that example (https://redis.io/commands/setbit):
Bitmaps are not an actual data type, but a set of bit-oriented operations defined on the String type (for more information refer to the Bitmaps section of the Data Types Introduction page). This means that bitmaps can be used with string commands, and most importantly with SET and GET.
Because Redis' strings are binary-safe, a bitmap is trivially encoded as a bytes stream. The first byte of the string corresponds to offsets 0..7 of the bitmap, the second byte to the 8..15 range, and so forth.
Related
How does redis bitcount command work?
setbit test 1 1
setbit test 3 1
count bits.
bitcount test
returns 2
Range count.
bitcount test 1 2
returns 0
Why? I thought I should get 1 for the bit position between 1 and 2, we have only one bit set.
The start and end arguments refer to bytes. So you're asking for the number of bits in the second and third bytes of the string. You've only set bits in the first byte, so the answer is 0.
It's unfortunate that the documentation for BITCOUNT doesn't explain that, but it is mentioned in the documentation for BITPOS:
It is possible to look for bits only in a specified interval passing the additional arguments start and end.... The range is interpreted as a range of bytes and not a range of bits, so start=0 and end=2 means to look at the first three bytes.
What does the statement "hexadecimal matches cleanly with modulo 8 word sizes, such as 8, 16, 32, and 64 bits" mean?
Since a single hex digit can represent exactly 4 bits of binary data, any word size that's a multiple of 4 can be exactly represented with a fixed number of hex digits.
And every word size that's a multiple of 8 (i.e. the common ones) can be represented with a number of digits that's a multiple of 2:
8 bits can store values from 00 to FF
16 bits can store values from 0000 to FFFF
32 bits can store values from 00000000 to FFFFFFFF
...
All 2-digit hex numbers can be represented in 8 bits and all 8 bit values can be represented in 2 hex digits. If a hex editor displays some value as CA FE BA BE you can easily grasp that it's 4 bytes and thus 32 bits. Getting that information from the decimal 3405707966 is not quite as trivial (no matter how you group the digits: there's no nice "byte boundaries" in that representation).
If you compare this with decimal, the same isn't true. For example, 8 bits can represent values from 0 to 255 (decimal). So you need up to 3 digits in decimal to represent 8-bit values. But there are 3-digit decimal values that you can't represent in 8 bits: 256 (or anything higher than that) doesn't map onto 8 bits. So the mapping isn't perfect for decimal numbers.
I have this line of code in modula-2 on a Big Endian processor :
Ptr := ADR(My_32_Bits_Integer)
I want to know if Ptr will be equal to the adress of the most significant byte (so the littlest address) or the less significant byte (so the biggest address) ?
The address of a 32 bit number is always that of the byte allocated first. On Big Endian systems, this is the MS byte, on Little Endian it is the LS byte.
Given the 32-bit integer 12345678h, then it will be stored like this:
Big Endian:
Offset Data
0 12
1 34
2 56
3 78
Little Endian:
Offset Data
0 78
1 56
2 34
3 12
I'm trying to build an parser to deserialze into object. Socket will send byte into parser. For the length of field 22 POS Entry Mode will N3 and byte will be always 2 digit. How to get the value for this field ?
You read the ASCII value of this field, and convert it into an integer.
if it says N3 that means they are three digits numeric field, so if the value say 51, you cast it to 051 and send the ASCII equivalent
Field 22 is pos entry mode. It's 3 digit numeric value. If format is BCD then 2 bytes contains 4 digits[ 0 (padded) + 3 digit POS entry mode). If format is ascci then it is 3 byte.
I'm trying to assemble proper track data given a CVC3 and a bunch of positional parameters. But the EMV C-2 Kernel book is about as obtuse as you could imagine (would it kill somebody to include an example!?!). Can anyone help work this example:
9f62 - pcvc3(t1) - Position of CVC3 in track1: 0x38 (4-6?)
9f63 - punatc(t1) - Unpredictable Number Track1 Pos: 0x3C6 (2-3 7-10?)
9f64 - natc(t1) - Digits in track1 ATC: 4
9f65 - pcvc3(t2) - Position of CVC3 in track2: 0x38 (4-6)
9f66 - punatc(t2) - Unpredictable Number Track2 Pos: 0x3C6 (2-3 7-10?)
9f67 - Digits in track2 ATC: 4
After successful checksum generation:
9f61 - track2 CVC3 - 2EF4
9f60 - track1 CVC3 - 609B
9f36 - ATC - 1E47
assuming the discretionary data field starts out as all 0s, how does it end up? The spec says this:
Convert the binary encoded CVC3 (Track2) to the BCD encoding of the
corresponding number expressed in base 10. Copy the q least significant digits of the
BCD encoded CVC3 (Track2) in the eligible positions of the 'Discretionary Data' in
Track 2 Data. The eligible positions are indicated by the q non-zero bits in
PCVC3(Track2).
I read that as:
CVC3 = 0x609B = 24731 (so copy 731? What does BCD have to do with this? Or are they just saying "copy the 731 as bcd encoded to the byte array"?)
yes you are correct it is rather obtuse. you are correct that you would convert your p values (pCVC3, and PUNATC) to binary. (0011 1000, 0011 1100 0110 for your track1 p values) you then right align the proper values with the discretionary data. example
Bxxxxxxxxxxxxxxxx^ /^14111014010000000000
....000000000000000000CCC000
....00000000000000AAAA000UU0
so you say that you CVC3 for track 1 is 609B which is 24,731, since you PCVC3 is asking for only 3 characters youd set 731 in. your ATC is 1E47 which is 7,751. your PUNATC is asking for 4 digits so you'd use 7751. FYI... if the ATC is lower than the requested characters you'd pad with 0's. Your unpredictable number is even more tricky... so you make a 4 byte random number. convert it to a uint (base 10) and then mark the 8 most significant bytes as 0. example. lets say your random 4 bytes is 29A6 06AE. in base 10 that is 698,746,542. mark out the first 8 characters with 0. and you are left with 000,000,002. you'd place 02 in for you unpredictable number placment.. so.. all that said your track would look like this
Bxxxxxxxxxxxxxxxx^ /^14111014017751731020
the last character is equal to the length of the unpredicatable number (numeric) digits. which was 02.. so the last digit is 2 making your final track data
%Bxxxxxxxxxxxxxxxx^ /^1411014017751731022;
track2 is very similar. good luck with it. I understand your frustrations with this. :)