Sign in to follow this  
Followers 0
Morthawt

Is there a simple way to convert a 32bit number into 8 chunks of 4bit?

23 posts in this topic

I am currently manually producing binary 1's and 0's and re-processing into 4bit integers. Is there some easier method without having to manually convert down to the 1's and 0's binary level first?

Share this post


Link to post
Share on other sites



Huh?


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

What about BitShift?

$i32bit = 123456789 ;0x075BCD15 = 0111.0101.1011.1100.1101.0001.0101
                    ;               7    5   11   12   13    1    5
For $i = 0 To 7
    ConsoleWrite(BitAND(0xF, BitShift($i32bit, 4 * $i)) & @CRLF)
Next

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Like, take this 32 bit number: 55028615

The binary representation of it is: 00000011010001111010101110000111

Then you break it down into groupings of 4:

0000
0011
0100
0111
1010
1011
1000
0111
 
Then break each 4 bit block into a 4 bit number:
0
3
4
7
10
11
8
7
 
Is there some easier method than manually converting down and processing individual bits?

Share this post


Link to post
Share on other sites

What about BitShift?

$i32bit = 123456789 ;0x075BCD15 = 0111.0101.1011.1100.1101.0001.0101
                    ;               7    5   11   12   13    1    5
For $i = 0 To 7
    ConsoleWrite(BitAND(0xF, BitShift($i32bit, 4 * $i)) & @CRLF)
Next

Br,

UEZ

 

That is looking very close to what I want. I just need to process and understand what it is doing. I think that would work, in reverse ordering.

Share this post


Link to post
Share on other sites

For $i = 7 To 0 Step - 1

Br,

UEZ ;)


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
For $i = 7 To 0 Step - 1

Br,

UEZ ;)

yes I got this done already. I am just trying to decipher what the code you wrote is doing. I am not too familiar with bitwise functions. I know bitand searches to see if a particular bit is flipped or not in a given number I think. But I am quite confused right now until I understand it. If you can provide an English explanation of what the functions are doing that would help me a lot most likely. Thanks. 

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

The 1. step is to shift the bits of the 32 bit number 4 * $i bits to the right. This is done by the BitShift($i32bit, 4 * $i) command.

To mask the result only to the last 4 bits (2^4-1 = 15) I used the BitAND() command.

BitAND(0xF, the result of the bitshift) = the result.

Ok?

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Erm... not really lol. I do not visually understand what it is doing. Plus I think BitAnd is the thing I use to check if a number has a particular binary bit flipped like finding out what options have been set on a combined number like option 1, option 2, option 4, option 8 etc. Then I can run a check to see if option 8 is part of that number. But in this situation I have no clue what bitand is doing or how the bits get shifted by the bitshift. I rarely ever deal with anything other than bitand, and in this situation my usage does not seem to fit with your usage.

Edited by Morthawt

Share this post


Link to post
Share on other sites

I think I understand the shift, it moves to the right so each time it cycles through the last 4 digits are discarded and the 4 prior are now at the end I think. But it seems like BitAnd may have two functions. I use it to determine if a paritcular bit was flipped to create the big number presented. If the number is not 0 then it was part of creating it. But it seems adding a hex 0xF is acting as a mask? Only giving a single first 4 bit grouping? Is this correct?

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Here a more detailed description:

1.  BitShift(123456789, 4 * 0) -> no shifting -> $result = 123456789 
    BitAND(0xF, 123456789) = 5
        0111.0101.1011.1100.1101.0001.0101
        0000.0000.0000.0000.0000.0000.1111
    =   0000.0000.0000.0000.0000.0000.0101 -> = 5

2.  BitShift(123456789, 4 * 1) -> shift the bits 4 times to the right -> 0000.0111.0101.1011.1100.1101.0001 = 7716049
    BitAND(0xF, 7716049) = 1
        0000.0111.0101.1011.1100.1101.0001
        0000.0000.0000.0000.0000.0000.1111
    =   0000.0000.0000.0000.0000.0000.0001 -> = 1
    
3.  BitShift(123456789, 4 * 2) -> shift the bits 8 times to the right -> 0000.0000.0111.0101.1011.1100.1101 = 482253
    BitAND(0xF, 482253) = 13
        0000.0000.0111.0101.1011.1100.1101
        0000.0000.0000.0000.0000.0000.1111
    =   0000.0000.0000.0000.0000.0000.1101 -> = 13
...


Eg: 1101 = 1*2^3 + 1*2^2 + 0*2^1 + 1*2^0 = 8 + 4 + 0 + 1 = 13
I hope it helps you to understand now.

 

 

And operation on bit level:

 

0 and 0 = 0

0 and 1 = 0

1 and 0 = 0

1 and 1 = 1

 

Br,

UEZ

Edited by UEZ
2 people like this

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Here a more detailed description:

1.  BitShift(123456789, 4 * 0) -> no shifting -> $result = 123456789 
    BitAND(0xF, 123456789) = 5
        0111.0101.1011.1100.1101.0001.0101
        0000.0000.0000.0000.0000.0000.FFFF
    =   0000.0000.0000.0000.0000.0000.0101 -> = 5

2.  BitShift(123456789, 4 * 1) -> shift the bits 4 times to the right -> 0000.0111.0101.1011.1100.1101.0001 = 7716049
    BitAND(0xF, 7716049) = 1
        0000.0111.0101.1011.1100.1101.0001
        0000.0000.0000.0000.0000.0000.FFFF
    =   0000.0000.0000.0000.0000.0000.0001 -> = 1
    
3.  BitShift(123456789, 4 * 2) -> shift the bits 8 times to the right -> 0000.0000.0111.0101.1011.1100.1101 = 482253
    BitAND(0xF, 482253) = 1
        0000.0000.0111.0101.1011.1100.1101
        0000.0000.0000.0000.0000.0000.FFFF
    =   0000.0000.0000.0000.0000.0000.1101 -> = 13
...


Eg: 1101 = 1*2^3 + 1*2^2 + 0*2^1 + 1*2^0

I hope it helps you to understand now.

 

And operation on bit level:

0 and 0 = 0

0 and 1 = 0

1 and 0 = 0

1 and 1 = 1

Br,

UEZ

I think so. So I was right, shift is lopping bits off the end and shifting the remaining over to the right (or left if you use negative number) and I think after some experimentation and looking at your depiction that I have a better understanding of BitAnd. I think BitAnd uses the first parameter to return all the values that are in the second parameter by checking only the bits used that are represented in the first parameter. So if on the first parameter I put in 3 and a number in the right it will only return the value based on the 1 and 2 bits, so it could produce a 0, 1, 2 or 3 since those bits can only produce those numbers. So if you use 0xF for the first or even 15, it returns the value of the second number but only up to the value produced by the status of the bits that can make up that first number, in this instance being 15 maximum.

Does that sound about right?

Edited by Morthawt

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

Sorry, I made a mistake by adding the FFFF. That makes no sense here! It must be 1111 (=15 or 0xF) of course!

But your understanding seems to be correct.

BitAnd masks the bits only. In this case you just cut off the bits from 32 to 4 because the 1st 4 bits are what you are looking for.

@J1: correct it is the same as the logic AND gate in electronics

Br,

UEZ

Edited by UEZ
1 person likes this

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Thank you UEZ. I love being able to improve my code with just these little elegant pieces of functionality. Once you truly understand the what and why, it is so much more useful. The help file does not really explain what the bitwise functions do very well. It assumes you know what they already are and just shows examples of them being used. I think better documentation would help. I might not have needed to make a post on the forum if I understood them from the beginning. I immediately went to the bitwise section but it was all greek to me. So I did a whole thing converting to bit binary and grouping things up etc :S very CPU time consuming compared to your method. So, thanks again for help me to understand it.

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

You are welcome guys!

PS: Bitshift to the right is also a division. In this case 4 bits to the rights means divison with 2^4 ;)

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0