A
Alef Veld
Hi everyone,
I'm trying to get my head around the concept of bit shifting, and there
are some things that seem to elude me. Many thanks for your answers.
1. When shifting bits, is there a certain bit size you are working on
(byte or more?) or can you define this yourself as with a bit field in
a struct. I mean i don't have to define a bit field when i shift a
number like 4 i can just do it. How many bits am i working with here.
Is that 32 bits for an int and a byte for a char ?
2. How far can you shift or divide ? To the max of the power of 2
within the boundaries of a unsigned int ? (i'm only doing shifts on
unsigned ints for now)
3. is the MSB always the left byte and the LSB always the right byte
(probably differs on endianness) ? Why are these 2 important anyway ?
4. When i shift 4 times, am i performing a decimal_number * 4 or am i
doing a decimal_number * decimal_number * decimal_number *
decimal_number ?
5. What does this code do ? :
if(sscanf(t_addr, "%d.%d.%d.%d",
&octet1, &octet2, &octet3, &octet4) < 1)
exit_error(-1);
return((octet1 << 24) | (octet2 << 16) | (octet3 << 8) | octet4);
I'm focussing on the last line here. The shifts would display to me the
logic of an 32bit ip address but would when one entered 192 for the
first octet the decimal also not get shifted by 24 getting 4608 ?which
makes not a lot of sense to me and i don't quite understand what the
reasoning for that is.
I'm used to working with ip addresses so for me i count from 128 and
start at 1. When working with more then 1 byte, do you just go to 256
512 1024 etc ? I tried a little program and it gave me this list after
the last number it went to 0 again. Is that the maximum amount of
multiplications that can be done ?
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216
33554432
67108864
134217728
268435456
536870912
1073741824
2147483648
0
Many thanks everyone
I'm trying to get my head around the concept of bit shifting, and there
are some things that seem to elude me. Many thanks for your answers.
1. When shifting bits, is there a certain bit size you are working on
(byte or more?) or can you define this yourself as with a bit field in
a struct. I mean i don't have to define a bit field when i shift a
number like 4 i can just do it. How many bits am i working with here.
Is that 32 bits for an int and a byte for a char ?
2. How far can you shift or divide ? To the max of the power of 2
within the boundaries of a unsigned int ? (i'm only doing shifts on
unsigned ints for now)
3. is the MSB always the left byte and the LSB always the right byte
(probably differs on endianness) ? Why are these 2 important anyway ?
4. When i shift 4 times, am i performing a decimal_number * 4 or am i
doing a decimal_number * decimal_number * decimal_number *
decimal_number ?
5. What does this code do ? :
if(sscanf(t_addr, "%d.%d.%d.%d",
&octet1, &octet2, &octet3, &octet4) < 1)
exit_error(-1);
return((octet1 << 24) | (octet2 << 16) | (octet3 << 8) | octet4);
I'm focussing on the last line here. The shifts would display to me the
logic of an 32bit ip address but would when one entered 192 for the
first octet the decimal also not get shifted by 24 getting 4608 ?which
makes not a lot of sense to me and i don't quite understand what the
reasoning for that is.
I'm used to working with ip addresses so for me i count from 128 and
start at 1. When working with more then 1 byte, do you just go to 256
512 1024 etc ? I tried a little program and it gave me this list after
the last number it went to 0 again. Is that the maximum amount of
multiplications that can be done ?
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216
33554432
67108864
134217728
268435456
536870912
1073741824
2147483648
0
Many thanks everyone